:root {
    --bg: #f0f0f3;
    --surface: #ffffff;
    --border: #e2e2e6;
    --text: #1c1c1e;
    --text-muted: #8e8e93;
    --accent: #4f46e5;
    --bubble-in: #e9e9eb;
    --danger: #dc2626;
}

@media (prefers-color-scheme: dark) {
    :root {
        --bg: #000000;
        --surface: #1c1c1e;
        --border: #38383a;
        --text: #f2f2f7;
        --text-muted: #98989f;
        --accent: #7c78f0;
        --bubble-in: #2c2c2e;
        --danger: #ff453a;
    }
}

* {
    box-sizing: border-box;
}

html, body {
    margin: 0;
    padding: 0;
    background: var(--bg);
    color: var(--text);
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
}

a {
    color: var(--accent);
    text-decoration: none;
}

.page {
    max-width: 640px;
    margin: 0 auto;
    min-height: 100vh;
    background: var(--surface);
    display: flex;
    flex-direction: column;
    /* Keep content clear of the home indicator in standalone mode. */
    padding-bottom: env(safe-area-inset-bottom, 0px);
}

.topbar {
    max-width: 640px;
    margin: 0 auto;
    width: 100%;
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 14px 16px;
    /* Installed on the Home Screen, the page draws under the notch/Dynamic
       Island (viewport-fit=cover) - push the header down below it. The
       plain padding above is the fallback for browsers without env()
       support; this just adds to it where safe-area-inset-top is > 0. */
    padding-top: calc(14px + env(safe-area-inset-top, 0px));
    background: var(--surface);
    border-bottom: 1px solid var(--border);
    position: sticky;
    top: 0;
    z-index: 10;
}

.brand {
    font-weight: 700;
    font-size: 1.1rem;
    color: var(--text);
}

.topnav {
    display: flex;
    gap: 16px;
    align-items: center;
}

.inline-form {
    display: contents;
}

.link-button {
    background: none;
    border: none;
    color: var(--accent);
    font: inherit;
    padding: 0;
    cursor: pointer;
}

/* --- Auth pages --- */

.auth-card {
    max-width: 360px;
    margin: 15vh auto;
    padding: 32px 24px;
    text-align: center;
}

.auth-card h1 {
    margin-bottom: 24px;
    font-size: 1.4rem;
}

.field {
    text-align: left;
    margin-bottom: 16px;
}

.field label {
    display: block;
    font-size: 0.85rem;
    color: var(--text-muted);
    margin-bottom: 4px;
}

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

.btn {
    display: inline-block;
    width: 100%;
    padding: 12px;
    border: none;
    border-radius: 10px;
    background: var(--accent);
    color: white;
    font-size: 1rem;
    font-weight: 600;
    cursor: pointer;
}

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

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

.validation-errors {
    color: var(--danger);
    font-size: 0.85rem;
    margin-bottom: 16px;
    text-align: left;
}

.muted-note {
    margin-top: 16px;
    font-size: 0.85rem;
    color: var(--text-muted);
}

/* --- Avatars --- */

.avatar {
    width: 48px;
    height: 48px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    color: white;
    font-weight: 700;
    font-size: 1rem;
    flex-shrink: 0;
}

.avatar.small {
    width: 32px;
    height: 32px;
    font-size: 0.8rem;
}

/* --- Conversations list --- */

.conversation-row {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 12px 16px;
    border-bottom: 1px solid var(--border);
}

.conversation-row:hover {
    background: var(--bg);
}

.conversation-main {
    flex: 1;
    min-width: 0;
}

.conversation-name {
    font-weight: 600;
    margin: 0 0 2px;
}

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

.conversation-meta {
    text-align: right;
    flex-shrink: 0;
}

.conversation-time {
    font-size: 0.8rem;
    color: var(--text-muted);
    display: block;
    margin-bottom: 4px;
}

.unread-badge {
    display: inline-block;
    min-width: 20px;
    padding: 2px 6px;
    border-radius: 10px;
    background: var(--accent);
    color: white;
    font-size: 0.75rem;
    font-weight: 700;
}

.empty-state {
    padding: 48px 24px;
    text-align: center;
    color: var(--text-muted);
}

/* --- Conversation thread --- */

.thread-header {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 12px 16px;
    border-bottom: 1px solid var(--border);
}

.thread-messages {
    flex: 1;
    padding: 16px;
    display: flex;
    flex-direction: column;
    gap: 12px;
}

.bubble {
    max-width: 78%;
    background: var(--bubble-in);
    border-radius: 18px;
    padding: 10px 14px;
    align-self: flex-start;
}

.bubble-title {
    font-weight: 700;
    margin-bottom: 2px;
}

.bubble-body {
    white-space: pre-wrap;
    word-break: break-word;
}

.bubble-time {
    font-size: 0.72rem;
    color: var(--text-muted);
    margin-top: 4px;
}

/* --- Profile / settings --- */

.section {
    padding: 20px 16px;
    border-bottom: 1px solid var(--border);
}

.section h2 {
    font-size: 1rem;
    margin: 0 0 12px;
}

.key-box {
    display: flex;
    align-items: center;
    gap: 8px;
    background: var(--bg);
    border-radius: 10px;
    padding: 10px 12px;
    font-family: ui-monospace, monospace;
    font-size: 0.9rem;
    word-break: break-all;
}

.status-line {
    margin-top: 10px;
    font-size: 0.9rem;
    color: var(--text-muted);
}

/* --- Admin --- */

.admin-table {
    width: 100%;
    border-collapse: collapse;
}

.admin-table th, .admin-table td {
    text-align: left;
    padding: 10px 8px;
    border-bottom: 1px solid var(--border);
    font-size: 0.9rem;
}

.token-reveal {
    /* Fixed light "warning" colors regardless of theme - explicit text
       color so it stays legible if --text flips light in dark mode. */
    background: #fffbea;
    color: #1c1c1e;
    border: 1px solid #f5d76e;
    border-radius: 10px;
    padding: 14px;
    margin: 16px;
    font-family: ui-monospace, monospace;
    word-break: break-all;
}

.pill {
    display: inline-block;
    padding: 2px 8px;
    border-radius: 999px;
    font-size: 0.75rem;
    font-weight: 600;
}

.pill.on {
    background: #dcfce7;
    color: #166534;
}

.pill.off {
    background: #fee2e2;
    color: #991b1b;
}
