:root {
    --bg: #0f172a;
    --panel: #1e293b;
    --panel-2: #273449;
    --accent: #38bdf8;
    --accent-2: #22c55e;
    --text: #e2e8f0;
    --muted: #94a3b8;
    --danger: #f87171;
    --border: #334155;
}

* { box-sizing: border-box; }

html {
    height: 100%;
    -webkit-text-size-adjust: 100%;
}

body {
    margin: 0;
    min-height: 100%;
    font-family: "Segoe UI", "PingFang SC", "Microsoft YaHei", system-ui, sans-serif;
    background: var(--bg);
    color: var(--text);
    overflow-x: hidden;
}

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

/* ---------- top bar ---------- */
.topbar {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    padding: 12px 20px;
    background: var(--panel);
    border-bottom: 1px solid var(--border);
    position: sticky;
    top: 0;
    z-index: 10;
}

.brand {
    font-size: 18px;
    font-weight: 700;
    min-width: 0;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}
.topbar nav {
    display: flex;
    align-items: center;
    gap: 16px;
    flex: 0 0 auto;
    font-size: 15px;
}

.status { font-size: 13px; color: var(--accent-2); }
.status.offline { color: var(--danger); }

/* ---------- chat ---------- */
body.chat-page {
    height: 100vh;
    height: 100dvh;
    overflow: hidden;
    display: flex;
    flex-direction: column;
}
body.chat-page .topbar { flex: 0 0 auto; }

.chat {
    display: flex;
    flex-direction: column;
    flex: 1 1 auto;
    min-height: 0;
    width: 100%;
    max-width: 920px;
    margin: 0 auto;
}

.messages {
    list-style: none;
    margin: 0;
    padding: 20px;
    flex: 1 1 auto;
    min-height: 0;
    overflow-y: auto;
    overflow-x: hidden;
    display: flex;
    flex-direction: column;
    gap: 14px;
}

.msg { display: flex; flex-direction: column; max-width: 78%; min-width: 0; }
.msg .meta { font-size: 12px; color: var(--muted); margin-bottom: 4px; }

.bubble {
    background: var(--panel);
    border: 1px solid var(--border);
    padding: 10px 14px;
    border-radius: 12px;
    white-space: pre-wrap;
    overflow-wrap: anywhere;
    word-break: break-word;
    line-height: 1.6;
    min-width: 0;
    max-width: 100%;
}

.msg.appear .bubble { animation: pop 0.25s ease; }
@keyframes pop {
    from { transform: translateY(6px); opacity: 0; }
    to   { transform: translateY(0);    opacity: 1; }
}

.composer {
    display: flex;
    gap: 10px;
    align-items: flex-end;
    flex: 0 0 auto;
    flex-wrap: nowrap;
    min-width: 0;
    padding: 14px 20px;
    padding-bottom: max(14px, env(safe-area-inset-bottom));
    background: var(--panel);
    border-top: 1px solid var(--border);
}

.composer input[type="text"],
.composer textarea {
    background: var(--panel-2);
    border: 1px solid var(--border);
    color: var(--text);
    padding: 11px 14px;
    border-radius: 10px;
    font-size: 15px;
    font-family: inherit;
    outline: none;
}
.composer input:focus,
.composer textarea:focus { border-color: var(--accent); }
#content {
    flex: 1 1 auto;
    min-width: 0;
    resize: vertical;
    min-height: 44px;
    max-height: 40vh;
    line-height: 1.5;
}

.btn {
    background: var(--accent);
    color: #06263a;
    border: none;
    padding: 11px 22px;
    border-radius: 10px;
    font-size: 15px;
    font-weight: 700;
    cursor: pointer;
}
.btn:hover { filter: brightness(1.08); }
.btn:disabled { opacity: 0.6; cursor: default; }

/* ---------- watch-only mode (server side WATCH UA detection) ----------
   In watch mode the composer is not rendered at all (read-only), so these
   rules increase readability instead of squeezing in an input row. */
/* Watch chat page: full-page scroll so the Digital Crown can scroll it,
   and a thin top bar that scrolls away with the content. */
body.chat-page.watch-mode {
    height: auto;
    overflow: visible;
    display: block;
}
body.chat-page.watch-mode .topbar {
    position: static;
    padding: 2px 6px;
    gap: 6px;
    min-height: 0;
}
body.chat-page.watch-mode .brand { font-size: 13px; line-height: 1.3; margin: 0; }
body.chat-page.watch-mode .topbar nav { gap: 8px; font-size: 12px; }
body.chat-page.watch-mode .topbar nav a { font-size: 12px; padding: 2px 4px; }
body.chat-page.watch-mode .status { display: none; }
body.watch-mode .messages {
    padding: 10px 12px;
    gap: 12px;
    overflow: visible;
    height: auto;
    flex: none;
    min-height: 0;
}
body.watch-mode .msg { max-width: 100%; }
body.watch-mode .bubble {
    padding: 10px 12px;
    font-size: 16px;
    line-height: 1.65;
}
body.watch-mode .bubble .content { font-size: 16px; }
body.watch-mode mjx-container { font-size: 15px; }
body.watch-mode .msg .meta { font-size: 13px; }

body.watch-mode .answers { padding: 14px 12px 56px; gap: 14px; }
body.watch-mode .card { padding: 16px 14px; }
body.watch-mode .card h2 { font-size: 17px; }
body.watch-mode .card p { font-size: 15px; }
body.watch-mode ol.tf, body.watch-mode ol.mc { gap: 12px; font-size: 17px; flex-wrap: wrap; }
body.watch-mode .mark { font-size: 20px; }
body.watch-mode table.diff { font-size: 14px; }
body.watch-mode table.diff th, body.watch-mode table.diff td { padding: 5px 3px; }

/* Answers page: thin top bar in watch mode as well */
body.answers-page.watch-mode .topbar {
    position: static;
    padding: 2px 6px;
    gap: 6px;
    min-height: 0;
}
body.answers-page.watch-mode .brand { font-size: 13px; line-height: 1.3; }
body.answers-page.watch-mode .topbar nav { gap: 8px; font-size: 12px; }
body.answers-page.watch-mode .topbar nav a { font-size: 12px; padding: 2px 4px; }

/* ---------- install ---------- */
.install-page {
    display: flex;
    align-items: center;
    justify-content: center;
    min-height: 100vh;
    padding: 20px;
}

.install-card {
    width: 100%;
    max-width: 440px;
    background: var(--panel);
    border: 1px solid var(--border);
    border-radius: 16px;
    padding: 28px;
}
.install-card h1 { margin: 0 0 6px; font-size: 22px; }
.install-card .sub { color: var(--muted); font-size: 14px; margin-top: 0; }
.install-card label { display: block; margin: 14px 0; font-size: 14px; color: var(--muted); }
.install-card input {
    display: block;
    width: 100%;
    margin-top: 6px;
    background: var(--panel-2);
    border: 1px solid var(--border);
    color: var(--text);
    padding: 11px 14px;
    border-radius: 10px;
    font-size: 15px;
    outline: none;
}
.install-card input:focus { border-color: var(--accent); }
.install-card .btn { width: 100%; margin-top: 10px; }

.errors {
    background: rgba(248, 113, 113, 0.12);
    border: 1px solid var(--danger);
    border-radius: 10px;
    padding: 10px 16px 10px 30px;
    color: var(--danger);
    font-size: 14px;
}
.ok { color: var(--accent-2); font-weight: 600; }

/* ---------- answers ---------- */
.answers {
    width: 100%;
    max-width: 860px;
    margin: 0 auto;
    padding: 24px 20px 60px;
    display: flex;
    flex-direction: column;
    gap: 18px;
}

.card {
    background: var(--panel);
    border: 1px solid var(--border);
    border-radius: 14px;
    padding: 22px 26px;
    min-width: 0;
}
.card h2 {
    margin: 0 0 12px;
    font-size: 17px;
    color: var(--accent);
    border-left: 4px solid var(--accent);
    padding-left: 10px;
}
.card p { line-height: 1.8; margin: 10px 0; min-width: 0; }
.card .lead { color: var(--muted); font-weight: 600; margin-bottom: 4px; }

/* MathJax SVG output: equations keep their full font size and wrap
   to multiple lines instead of overflowing or being scaled down. */
mjx-container[jax="SVG"] { max-width: 100%; }
mjx-container.MathJax[jax="SVG"][display="true"] {
    display: block;
    text-align: center;
    max-width: 100%;
    overflow: hidden;
    padding: 2px 0;
}
mjx-container.MathJax[jax="SVG"][display="true"] > svg,
mjx-container.MathJax[jax="SVG"] > svg {
    max-width: 100%;
    vertical-align: baseline;
}

/* Wide tables compress to fit instead of scrolling */
.table-scroll {
    margin: 0;
}

table.diff {
    width: 100%;
    table-layout: fixed;
    border-collapse: collapse;
    margin: 14px 0;
    font-size: 15px;
}
table.diff th, table.diff td {
    border: 1px solid var(--border);
    padding: 6px 4px;
    text-align: center;
    overflow-wrap: break-word;
    word-break: break-word;
}
table.diff thead th { background: var(--panel-2); color: var(--accent); }

@media (max-width: 560px) {
    .topbar { padding: 10px 14px; gap: 8px; }
    .brand { font-size: 16px; }
    .topbar nav { gap: 12px; font-size: 14px; }

    .msg { max-width: 92%; }

    .messages { padding: 14px; gap: 12px; }
    .composer { padding: 10px 12px; gap: 8px; }

    #content { font-size: 14px; padding: 10px 10px; }
    .composer .btn { padding: 10px 14px; font-size: 14px; }

    .answers { padding: 14px 12px 48px; gap: 14px; }
    .card { padding: 16px 14px; }
    .card h2 { font-size: 16px; }
    .card p { font-size: 15px; }

    ol.tf, ol.mc { gap: 16px; font-size: 16px; }
    .mark { font-size: 20px; }

    .install-page { padding: 14px; }
    .install-card { padding: 22px 18px; }
}

@media (max-width: 360px) {
    ol.tf, ol.mc { gap: 12px; }
}

/* Apple Watch / ultra-narrow (<= 340px logical width) */
@media (max-width: 340px) {
    .topbar { padding: 8px 10px; gap: 6px; }
    .brand { font-size: 14px; max-width: 55%; }
    .topbar nav { gap: 8px; font-size: 12px; }

    .msgs { padding: 10px; }
    .messages { padding: 10px; gap: 10px; }
    .msg { max-width: 96%; }
    .bubble { padding: 8px 10px; font-size: 13px; }

    /* composer: input + send on one row */
    .composer {
        flex-wrap: nowrap;
        padding: 8px 10px;
        gap: 7px;
    }
    #content { flex: 1 1 auto; min-height: 40px; font-size: 13px; }
    .composer .btn { padding: 10px 12px; font-size: 13px; }

    .answers { padding: 10px; gap: 12px; }
    .card { padding: 12px 10px; }
    .card h2 { font-size: 14px; }
    .card p { font-size: 13px; }

    ol.tf, ol.mc { gap: 8px; font-size: 14px; flex-wrap: wrap; }
    .mark { font-size: 17px; }

    .install-page { padding: 10px; }
    .install-card { padding: 18px 14px; }
}
