/* Deadrop Web App Styles */

:root {
    --primary-color: #2563eb;
    --primary-hover: #1d4ed8;
    --danger-color: #dc2626;
    --warning-color: #f59e0b;
    --success-color: #10b981;
    --bg-color: #f9fafb;
    --card-bg: #ffffff;
    --text-color: #1f2937;
    --text-muted: #6b7280;
    --border-color: #e5e7eb;
    --shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
    --radius: 8px;
}

* {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
}

body {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
    background: var(--bg-color);
    color: var(--text-color);
    line-height: 1.5;
    min-height: 100vh;
}

/* Layout */
.container {
    max-width: 800px;
    margin: 0 auto;
    padding: 20px;
}

.header {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 16px 20px;
    background: var(--card-bg);
    border-bottom: 1px solid var(--border-color);
    position: sticky;
    top: 0;
    z-index: 100;
}

.header h1 {
    font-size: 1.25rem;
    font-weight: 600;
}

.header .subtitle {
    color: var(--text-muted);
    font-size: 0.875rem;
}

.back-link {
    color: var(--text-muted);
    text-decoration: none;
    font-size: 1.25rem;
}

.back-link:hover {
    color: var(--primary-color);
}

/* Cards */
.card {
    background: var(--card-bg);
    border: 1px solid var(--border-color);
    border-radius: var(--radius);
    padding: 16px;
    margin-bottom: 12px;
    box-shadow: var(--shadow);
}

.card:hover {
    border-color: var(--primary-color);
}

.card-clickable {
    cursor: pointer;
    transition: border-color 0.15s;
}

.card-title {
    font-weight: 600;
    margin-bottom: 4px;
}

.card-subtitle {
    color: var(--text-muted);
    font-size: 0.875rem;
}

/* Buttons */
.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    padding: 10px 20px;
    border: none;
    border-radius: var(--radius);
    font-size: 1rem;
    font-weight: 500;
    cursor: pointer;
    transition: background-color 0.15s;
}

.btn-primary {
    background: var(--primary-color);
    color: white;
}

.btn-primary:hover {
    background: var(--primary-hover);
}

.btn-secondary {
    background: var(--border-color);
    color: var(--text-color);
}

.btn-secondary:hover {
    background: #d1d5db;
}

.btn-danger {
    background: var(--danger-color);
    color: white;
}

.btn-danger:hover {
    background: #b91c1c;
}

.btn-small {
    padding: 6px 12px;
    font-size: 0.875rem;
}

.btn-icon {
    padding: 8px;
    background: transparent;
    border: 1px solid var(--border-color);
}

.btn-icon:hover {
    background: var(--border-color);
}

/* Forms */
.form-group {
    margin-bottom: 16px;
}

.form-label {
    display: block;
    margin-bottom: 6px;
    font-weight: 500;
    font-size: 0.875rem;
}

.form-input {
    width: 100%;
    padding: 10px 12px;
    border: 1px solid var(--border-color);
    border-radius: var(--radius);
    font-size: 1rem;
}

.form-input:focus {
    outline: none;
    border-color: var(--primary-color);
    box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.1);
}

textarea.form-input {
    resize: vertical;
    min-height: 100px;
}

/* Message bubbles */
.message-list {
    display: flex;
    flex-direction: column-reverse;  /* Reverse for natural scroll-to-bottom */
    gap: 8px;
    padding: 20px;
    overflow-y: auto;
    flex: 1;
    min-height: 0;  /* Allow shrinking in flex container */
}

.message {
    max-width: 70%;
    padding: 12px 16px;
    border-radius: 16px;
    position: relative;
}

.message-sent {
    align-self: flex-start;
    background: var(--border-color);
    border-bottom-left-radius: 4px;
}

.message-received {
    align-self: flex-end;
    background: var(--primary-color);
    color: white;
    border-bottom-right-radius: 4px;
}

.message-body {
    word-wrap: break-word;
}

.message-meta {
    display: flex;
    gap: 8px;
    margin-top: 6px;
    font-size: 0.75rem;
    opacity: 0.7;
}

.message-actions {
    display: none;
    gap: 4px;
    margin-top: 8px;
}

.message:hover .message-actions {
    display: flex;
}

/* TTL indicator */
.ttl-indicator {
    display: inline-flex;
    align-items: center;
    gap: 4px;
    font-size: 0.75rem;
    color: var(--text-muted);
}

.ttl-warning {
    color: var(--warning-color);
}

.ttl-urgent {
    color: var(--danger-color);
}

/* Unified thread list (1:1 conversations + rooms) */
#thread-list {
    overflow-y: auto;
    flex: 1;
    min-height: 0;
}

.thread-item {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 14px 16px;
    border-bottom: 1px solid var(--border-color);
    cursor: pointer;
    transition: background-color 0.15s;
}

.thread-item:hover {
    background: var(--bg-color);
}

.thread-item.unread {
    background: rgba(37, 99, 235, 0.05);
}

.thread-avatar {
    width: 44px;
    height: 44px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    font-weight: 600;
    flex-shrink: 0;
}

.thread-avatar-peer {
    background: var(--primary-color);
    color: white;
    font-size: 1.2rem;
}

.thread-avatar-room {
    background: linear-gradient(135deg, var(--primary-color), #7c3aed);
    color: white;
    font-size: 1.1rem;
}

.thread-content {
    flex: 1;
    min-width: 0;
}

.thread-header {
    display: flex;
    justify-content: space-between;
    align-items: baseline;
    margin-bottom: 3px;
}

.thread-name {
    font-weight: 600;
}

.thread-time {
    font-size: 0.75rem;
    color: var(--text-muted);
    flex-shrink: 0;
    margin-left: 8px;
}

.thread-preview {
    font-size: 0.875rem;
    color: var(--text-muted);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.unread-dot {
    width: 10px;
    height: 10px;
    background: var(--primary-color);
    border-radius: 50%;
    flex-shrink: 0;
}

/* Namespace mode badges */
.mode-badge {
    display: inline-flex;
    align-items: center;
    gap: 4px;
    padding: 2px 8px;
    border-radius: 12px;
    font-size: 0.75rem;
    font-weight: 500;
}

.mode-badge.ephemeral {
    background: rgba(245, 158, 11, 0.1);
    color: var(--warning-color);
}

.mode-badge.persistent {
    background: rgba(16, 185, 129, 0.1);
    color: var(--success-color);
}

/* Compose area */
.compose-area {
    display: flex;
    gap: 12px;
    padding: 16px 20px;
    background: var(--card-bg);
    border-top: 1px solid var(--border-color);
    position: sticky;
    bottom: 0;
}

.compose-input {
    flex: 1;
    padding: 12px 16px;
    border: 1px solid var(--border-color);
    border-radius: 24px;
    font-size: 1rem;
    resize: none;
    max-height: 120px;
}

.compose-input:focus {
    outline: none;
    border-color: var(--primary-color);
}

/* Empty states */
.empty-state {
    text-align: center;
    padding: 60px 20px;
    color: var(--text-muted);
}

.empty-state-icon {
    font-size: 3rem;
    margin-bottom: 16px;
}

.empty-state-title {
    font-size: 1.25rem;
    font-weight: 600;
    margin-bottom: 8px;
    color: var(--text-color);
}

/* Invite page */
.invite-container {
    max-width: 480px;
    margin: 80px auto;
    padding: 20px;
}

.invite-card {
    background: var(--card-bg);
    border-radius: var(--radius);
    box-shadow: var(--shadow);
    padding: 32px;
    text-align: center;
}

.invite-icon {
    font-size: 3rem;
    margin-bottom: 16px;
}

.invite-title {
    font-size: 1.5rem;
    font-weight: 600;
    margin-bottom: 24px;
}

.invite-details {
    text-align: left;
    margin-bottom: 24px;
}

.invite-detail {
    display: flex;
    justify-content: space-between;
    padding: 12px 0;
    border-bottom: 1px solid var(--border-color);
}

.invite-detail:last-child {
    border-bottom: none;
}

.invite-detail-label {
    color: var(--text-muted);
}

.invite-detail-value {
    font-weight: 500;
}

.invite-warning {
    font-size: 0.875rem;
    color: var(--text-muted);
    margin-top: 16px;
}

/* Error/Loading states */
.error-message {
    background: rgba(220, 38, 38, 0.1);
    color: var(--danger-color);
    padding: 12px 16px;
    border-radius: var(--radius);
    margin-bottom: 16px;
}

.loading {
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 40px;
    color: var(--text-muted);
}

.spinner {
    width: 24px;
    height: 24px;
    border: 3px solid var(--border-color);
    border-top-color: var(--primary-color);
    border-radius: 50%;
    animation: spin 1s linear infinite;
}

@keyframes spin {
    to { transform: rotate(360deg); }
}

/* Utilities */
.hidden {
    display: none !important;
}

.text-muted {
    color: var(--text-muted);
}

.text-small {
    font-size: 0.875rem;
}

.mt-2 { margin-top: 8px; }
.mt-4 { margin-top: 16px; }
.mb-2 { margin-bottom: 8px; }
.mb-4 { margin-bottom: 16px; }

/* Responsive */
@media (max-width: 640px) {
    .container {
        padding: 12px;
    }
    
    .message {
        max-width: 85%;
    }
    
    .invite-container {
        margin: 40px auto;
    }
}

/* ==================== ROOM STYLES ==================== */

/* Room Message (with sender) */
.room-message {
    padding: 8px 16px;
    width: 100%;
    box-sizing: border-box;
}

.room-message .sender-name {
    font-size: 12px;
    font-weight: 600;
    color: var(--primary-color);
    margin-bottom: 4px;
}

.room-message .message-body {
    font-size: 15px;
    line-height: 1.5;
    word-wrap: break-word;
}

/* Plain text messages preserve whitespace */
.room-message .message-body:not(.markdown-body) {
    white-space: pre-wrap;
}

/* ========== MARKDOWN CONTENT STYLES ========== */

/* Reset markdown-body spacing inside chat bubbles */
.message-body.markdown-body {
    font-family: inherit;
    font-size: inherit;
}

.message-body.markdown-body > *:first-child {
    margin-top: 0;
}

.message-body.markdown-body > *:last-child {
    margin-bottom: 0;
}

.message-body.markdown-body p {
    margin: 0.4em 0;
}

/* Inline code */
.message-body.markdown-body code {
    background: rgba(0, 0, 0, 0.06);
    padding: 2px 6px;
    border-radius: 4px;
    font-family: 'SF Mono', 'Menlo', 'Monaco', 'Courier New', monospace;
    font-size: 0.88em;
}

/* Code blocks */
.message-body.markdown-body pre {
    background: #1e1e2e;
    color: #cdd6f4;
    padding: 14px 16px;
    border-radius: var(--radius);
    overflow-x: auto;
    margin: 8px 0;
    font-size: 13px;
    line-height: 1.5;
    -webkit-overflow-scrolling: touch;
}

.message-body.markdown-body pre code {
    background: none;
    padding: 0;
    border-radius: 0;
    font-size: inherit;
    color: inherit;
}

/* Override highlight.js background inside our dark pre */
.message-body.markdown-body pre code.hljs {
    background: transparent;
    padding: 0;
}

/* Blockquotes */
.message-body.markdown-body blockquote {
    border-left: 3px solid var(--primary-color);
    margin: 8px 0;
    padding: 4px 12px;
    color: var(--text-muted);
}

.message-body.markdown-body blockquote p {
    margin: 0.2em 0;
}

/* Lists */
.message-body.markdown-body ul,
.message-body.markdown-body ol {
    margin: 4px 0;
    padding-left: 24px;
}

.message-body.markdown-body li {
    margin: 2px 0;
}

/* Tables */
.message-body.markdown-body table {
    border-collapse: collapse;
    margin: 8px 0;
    font-size: 14px;
    width: 100%;
    overflow-x: auto;
    display: block;
}

.message-body.markdown-body th,
.message-body.markdown-body td {
    border: 1px solid var(--border-color);
    padding: 6px 12px;
    text-align: left;
}

.message-body.markdown-body th {
    background: rgba(0, 0, 0, 0.04);
    font-weight: 600;
}

/* Headings inside messages — scale down */
.message-body.markdown-body h1 { font-size: 1.3em; margin: 0.5em 0 0.3em; }
.message-body.markdown-body h2 { font-size: 1.15em; margin: 0.5em 0 0.3em; }
.message-body.markdown-body h3 { font-size: 1.05em; margin: 0.4em 0 0.2em; }
.message-body.markdown-body h4,
.message-body.markdown-body h5,
.message-body.markdown-body h6 { font-size: 1em; margin: 0.4em 0 0.2em; }

/* Links */
.message-body.markdown-body a {
    color: var(--primary-color);
    text-decoration: underline;
}

/* Horizontal rules */
.message-body.markdown-body hr {
    border: none;
    border-top: 1px solid var(--border-color);
    margin: 8px 0;
}

/* Images — clickable thumbnails */
.message-body.markdown-body img.md-image {
    max-width: 100%;
    max-height: 300px;
    border-radius: var(--radius);
    cursor: zoom-in;
    margin: 6px 0;
    display: block;
}

/* Strikethrough */
.message-body.markdown-body del {
    text-decoration: line-through;
    opacity: 0.7;
}

/* From-me messages are plain text, no markdown overrides needed */

/* ========== REACTION BADGES ========== */

.reaction-badges {
    display: flex;
    flex-wrap: wrap;
    gap: 4px;
    margin-top: 4px;
}

.reaction-badge {
    display: inline-flex;
    align-items: center;
    gap: 2px;
    padding: 2px 8px;
    border-radius: 12px;
    border: 1px solid var(--border-color);
    background: var(--bg-color);
    font-size: 14px;
    cursor: pointer;
    transition: all 0.15s ease;
    line-height: 1.4;
}

.reaction-badge:hover {
    background: var(--hover-bg);
    border-color: var(--primary-color);
}

.reaction-badge.mine {
    background: rgba(37, 99, 235, 0.1);
    border-color: var(--primary-color);
}

.reaction-badge .reaction-count {
    font-size: 12px;
    color: var(--text-muted);
}

.reaction-badge.add-reaction {
    font-size: 12px;
    color: var(--text-muted);
    padding: 2px 6px;
    opacity: 0.4;
    transition: opacity 0.15s ease;
}

.room-message:hover .reaction-badge.add-reaction {
    opacity: 1;
}

/* Touch devices: always show the add-reaction button since there's no hover */
@media (hover: none) {
    .reaction-badge.add-reaction {
        opacity: 0.6;
    }
}

/* From-me reactions: adjust for blue bubble context */
.room-message.from-me .reaction-badges {
    justify-content: flex-end;
}

/* Reaction picker popup */
.reaction-picker {
    display: flex;
    gap: 2px;
    padding: 6px 8px;
    background: white;
    border-radius: 24px;
    box-shadow: 0 4px 16px rgba(0, 0, 0, 0.15);
    border: 1px solid var(--border-color);
}

.reaction-picker-btn {
    font-size: 22px;
    padding: 4px 6px;
    background: none;
    border: none;
    border-radius: 8px;
    cursor: pointer;
    transition: background 0.1s;
    line-height: 1;
}

.reaction-picker-btn:hover {
    background: var(--hover-bg);
}

.room-message .message-time {
    font-size: 11px;
    color: var(--text-muted);
    margin-top: 4px;
}

/* Own messages - compact bubble style on right */
.room-message.from-me {
    display: flex;
    flex-direction: column;
    align-items: flex-end;
}

.room-message.from-me .sender-name {
    color: var(--text-muted);
}

.room-message.from-me .message-body {
    display: inline-block;
    max-width: 80%;
    padding: 10px 14px;
    border-radius: var(--radius);
    background: var(--primary-color);
    color: white;
    white-space: pre-wrap;
}

.room-message.from-me .message-time {
    color: var(--text-muted);
}

/* Actions toolbar */
.toolbar {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 12px 20px;
    border-bottom: 1px solid var(--border-color);
}

.toolbar-actions {
    display: flex;
    gap: 8px;
}

/* Pending message state */
.room-message.pending {
    opacity: 0.6;
}

.room-message.pending .message-body {
    background: var(--bg-color);
    border: 1px dashed var(--border-color);
}

.room-message.pending.from-me .message-body {
    background: rgba(37, 99, 235, 0.5);
    border: 1px dashed var(--primary-color);
}

/* Disabled send button */
button:disabled {
    opacity: 0.6;
    cursor: not-allowed;
}

/* View layout for proper scrolling */
.view {
    display: flex;
    flex-direction: column;
    height: 100vh;
    max-height: 100vh;
    overflow: hidden;
}

.view.hidden {
    display: none !important;
}

/* Ensure header doesn't shrink */
.view > .header {
    flex-shrink: 0;
}

/* Compose area at bottom, doesn't shrink */
.view > .compose-area {
    flex-shrink: 0;
}
