:root {
    --bg: #f3f6fb;
    --surface: #ffffff;
    --surface-2: #f8fafc;
    --code-bg: #eef2f8;
    --text: #16202e;
    --muted: #5b6b80;
    --border: #dbe3ee;
    --primary: #1d4ed8;
    --primary-hover: #1e40af;
    --danger: #b42318;
    --success-bg: #e7f6ec;
    --success-text: #14532d;
    --error-bg: #fdecec;
    --error-text: #8a1c12;
    --radius: 12px;
    --shadow: 0 1px 2px rgba(16, 24, 40, .05), 0 8px 24px rgba(16, 24, 40, .06);
}

@media (prefers-color-scheme: dark) {
    :root {
        --bg: #0d1220;
        --surface: #151d2e;
        --surface-2: #1b2537;
        --code-bg: #0f1728;
        --text: #e6ecf5;
        --muted: #9aabc2;
        --border: #2a3a55;
        --primary: #6d9bff;
        --primary-hover: #8db2ff;
        --danger: #ff7b72;
        --success-bg: #11301f;
        --success-text: #9ae6b4;
        --error-bg: #3a1414;
        --error-text: #ffb4ab;
        --shadow: 0 1px 2px rgba(0, 0, 0, .4), 0 8px 24px rgba(0, 0, 0, .35);
    }
}

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

body {
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue",
                 "Noto Sans", "PingFang TC", "Microsoft JhengHei", "Microsoft YaHei", sans-serif;
    background: var(--bg);
    color: var(--text);
    line-height: 1.55;
    -webkit-text-size-adjust: 100%;
}

a { color: var(--primary); text-decoration: none; }
a:hover { text-decoration: underline; }
.wrap { width: 100%; max-width: 1080px; margin: 0 auto; padding: 0 1rem; }

.topbar {
    background: var(--surface);
    border-bottom: 1px solid var(--border);
    position: sticky;
    top: 0;
    z-index: 10;
}
.topbar-inner {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 1rem;
    min-height: 60px;
    flex-wrap: wrap;
    padding-top: .5rem;
    padding-bottom: .5rem;
}
.brand { display: inline-flex; align-items: center; gap: .55rem; color: var(--text); font-weight: 700; }
.brand:hover { text-decoration: none; }
.brand-mark {
    display: inline-flex; align-items: center; justify-content: center;
    width: 32px; height: 32px; border-radius: 9px;
    background: var(--primary); color: #fff; font-size: .72rem; font-weight: 800;
}
.topnav { display: flex; align-items: center; gap: 1rem; flex-wrap: wrap; }
.topnav a { color: var(--muted); font-weight: 600; }
.topnav a:hover { color: var(--text); text-decoration: none; }
.who { color: var(--muted); font-size: .85rem; }

.main { padding: 1.5rem 1rem 3rem; }

.card {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    box-shadow: var(--shadow);
    padding: 1.25rem;
    margin-bottom: 1.25rem;
}

h1 { font-size: 1.5rem; margin: 0 0 .35rem; letter-spacing: -.01em; }
h2 { font-size: 1.1rem; margin: 1.1rem 0 .75rem; }
h3 { font-size: 1rem; }
p { margin: .35rem 0 .9rem; }
.muted { color: var(--muted); }
.mono { font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, "Liberation Mono", monospace; }
.breadcrumb { margin: 0 0 1rem; font-weight: 600; }

.hero {
    display: flex; justify-content: space-between; align-items: flex-end;
    gap: 1rem; flex-wrap: wrap; margin-bottom: 1rem;
}
.hero h1 { margin: 0; }
.sync { color: var(--muted); font-size: .85rem; }

.stats {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
    gap: 1rem;
    margin-bottom: 1.25rem;
}
.stat {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 1rem;
    box-shadow: var(--shadow);
}
.stat span { display: block; color: var(--muted); font-size: .78rem; text-transform: uppercase; letter-spacing: .04em; }
.stat strong { font-size: 1.8rem; }

.alert { border-radius: 10px; padding: .7rem .9rem; margin: 0 0 1rem; font-size: .92rem; border: 1px solid transparent; }
.alert-error { background: var(--error-bg); color: var(--error-text); border-color: currentColor; }
.alert-success { background: var(--success-bg); color: var(--success-text); border-color: currentColor; }

label { display: block; font-weight: 600; font-size: .9rem; }
label span { display: block; margin-bottom: .3rem; }
input[type="text"], input[type="password"], textarea, select {
    width: 100%;
    padding: .6rem .7rem;
    border: 1px solid var(--border);
    border-radius: 9px;
    background: var(--surface-2);
    color: var(--text);
    font-size: 1rem;
    font-family: inherit;
}
input:focus, textarea:focus, select:focus, button:focus-visible, a:focus-visible {
    outline: 2px solid var(--primary);
    outline-offset: 2px;
    border-color: var(--primary);
}
textarea { resize: vertical; }

.stack { display: grid; gap: 1rem; }
.stack .button { width: 100%; }

.button {
    display: inline-flex; align-items: center; justify-content: center; gap: .35rem;
    padding: .55rem .95rem;
    border-radius: 9px;
    border: 1px solid var(--border);
    background: var(--surface-2);
    color: var(--text);
    font: inherit; font-weight: 600; cursor: pointer; text-decoration: none;
    line-height: 1.2;
}
.button:hover { text-decoration: none; border-color: var(--muted); }
.button.primary { background: var(--primary); border-color: var(--primary); color: #fff; }
.button.primary:hover { background: var(--primary-hover); border-color: var(--primary-hover); }
.button.danger { background: transparent; border-color: var(--danger); color: var(--danger); }
.button.danger:hover { background: var(--danger); color: #fff; }
.button.small { padding: .35rem .6rem; font-size: .82rem; }

.link-button { background: none; border: 0; padding: 0; color: var(--muted); font: inherit; font-weight: 600; cursor: pointer; }
.link-button:hover { color: var(--text); }
.inline { display: inline; margin: 0; }

.table-wrap { overflow-x: auto; -webkit-overflow-scrolling: touch; }
table { width: 100%; border-collapse: collapse; }
th, td { padding: .65rem .6rem; border-bottom: 1px solid var(--border); text-align: left; vertical-align: top; }
th { font-size: .78rem; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); }
tr:last-child td { border-bottom: 0; }

.badge {
    display: inline-block; padding: .12rem .45rem; border-radius: 999px;
    background: var(--surface-2); border: 1px solid var(--border);
    font-size: .76rem; font-weight: 700; letter-spacing: .03em;
}
.badge.you { background: var(--primary); border-color: var(--primary); color: #fff; }
.badge.pin { border-color: var(--primary); color: var(--primary); }

.notes { display: grid; gap: .8rem; }
.note { background: var(--surface-2); border: 1px solid var(--border); border-radius: 10px; padding: 1rem; }
.note h3 { margin: 0 0 .4rem; }
.note p { margin: 0; }
.note-head { display: flex; justify-content: space-between; gap: 1rem; align-items: flex-start; }
.note-foot { display: flex; justify-content: space-between; align-items: center; gap: 1rem; margin-top: .8rem; }

.days { display: grid; gap: .5rem; }
.day-row {
    display: flex; justify-content: space-between; gap: 1rem;
    padding: .7rem .9rem; border: 1px solid var(--border); border-radius: 9px;
    color: var(--text); background: var(--surface-2);
}
.day-row:hover { border-color: var(--primary); text-decoration: none; }
.day-date { font-weight: 700; font-family: ui-monospace, Menlo, Consolas, monospace; }
.day-meta { color: var(--muted); font-size: .88rem; }

.chips { display: flex; flex-wrap: wrap; gap: .5rem; }
.chip { border: 1px solid var(--border); background: var(--surface-2); border-radius: 999px; padding: .25rem .7rem; font-size: .85rem; }

.timeline { display: grid; gap: .8rem; }
.titem { border: 1px solid var(--border); border-left: 4px solid var(--border); border-radius: 10px; padding: .8rem 1rem; background: var(--surface-2); }
.titem.user { border-left-color: var(--primary); }
.titem.assistant { border-left-color: #16a34a; }
.titem.tool { border-left-color: #d97706; }
.tmeta { display: flex; align-items: center; gap: .6rem; margin-bottom: .45rem; flex-wrap: wrap; }
.tmeta .time, .tmeta .sid { color: var(--muted); font-size: .78rem; }
.titem .text { white-space: pre-wrap; overflow-wrap: anywhere; }
.cmd { margin: .2rem 0 0; padding: .6rem .7rem; background: var(--code-bg); border-radius: 8px; white-space: pre-wrap; overflow-wrap: anywhere; font-size: .85rem; }
.status { font-size: .75rem; color: var(--muted); }

.meta-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(180px, 1fr)); gap: .8rem; margin: 0 0 1rem; }
.meta-grid dt { color: var(--muted); font-size: .78rem; text-transform: uppercase; letter-spacing: .04em; }
.meta-grid dd { margin: .15rem 0 0; font-weight: 600; }

.searchbar { display: flex; gap: .6rem; flex-wrap: wrap; }
.searchbar input { flex: 1 1 240px; }

.checkbox { display: flex; align-items: center; gap: .5rem; font-weight: 500; }
.checkbox input { width: auto; }
.checkbox span { margin: 0; }

.auth-card { max-width: 420px; margin: 4vh auto 0; }
.footer { padding-top: 1rem; padding-bottom: 2rem; color: var(--muted); font-size: .8rem; }

@media (max-width: 720px) {
    .hero { align-items: flex-start; }
    .table-wrap { overflow: visible; }
    table, thead, tbody, tr, td { display: block; width: 100%; }
    thead { display: none; }
    tbody tr {
        border: 1px solid var(--border);
        border-radius: 10px;
        margin-bottom: .7rem;
        padding: .35rem .65rem;
        background: var(--surface-2);
    }
    td {
        display: flex;
        justify-content: space-between;
        align-items: flex-start;
        gap: 1rem;
        border-bottom: 1px dashed var(--border);
        padding: .5rem 0;
        text-align: right;
    }
    td:last-child { border-bottom: 0; }
    td::before {
        content: attr(data-label);
        font-size: .72rem;
        text-transform: uppercase;
        letter-spacing: .04em;
        color: var(--muted);
        font-weight: 700;
        text-align: left;
        flex: 0 0 42%;
    }
}

.summary { color: var(--muted); margin: -.4rem 0 1.25rem; }

/* Chat */
.chat-header { display: flex; justify-content: space-between; align-items: flex-start; gap: 1rem; flex-wrap: wrap; }
.chat-heading h1 { margin: 0 0 .2rem; }
.chat-heading p { margin: 0; }
.chat-actions { display: flex; gap: .5rem; align-items: center; flex-wrap: wrap; }
.session-picker { position: relative; }
.session-picker summary {
    cursor: pointer; font-weight: 600; padding: .45rem .7rem;
    border: 1px solid var(--border); border-radius: 9px; background: var(--surface-2);
}
.session-list {
    position: absolute; right: 0; top: calc(100% + .4rem); z-index: 20;
    width: min(420px, 82vw); max-height: 320px; overflow-y: auto;
    background: var(--surface); border: 1px solid var(--border);
    border-radius: 10px; box-shadow: var(--shadow); padding: .35rem;
}
.session-list a {
    display: flex; justify-content: space-between; gap: .6rem; align-items: center;
    padding: .5rem .6rem; border-radius: 7px; color: var(--text);
}
.session-list a:hover { background: var(--surface-2); text-decoration: none; }
.session-list a.current { background: var(--surface-2); }
.session-title { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chat-messages {
    height: min(60vh, 620px); overflow-y: auto;
    display: flex; flex-direction: column; gap: .75rem;
    padding: 1rem; background: var(--surface);
    border: 1px solid var(--border); border-radius: var(--radius);
    box-shadow: var(--shadow); margin-bottom: .75rem;
}
.msg { max-width: 88%; padding: .7rem .9rem; border-radius: 12px; border: 1px solid var(--border); }
.msg-user { align-self: flex-end; background: var(--primary); border-color: var(--primary); color: #fff; }
.msg-user .msg-head { color: rgba(255,255,255,.75); }
.msg-assistant { align-self: flex-start; background: var(--surface-2); }
.msg-head { font-size: .72rem; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); margin-bottom: .35rem; }
.msg-text { white-space: pre-wrap; overflow-wrap: anywhere; }
.tool-list { display: flex; flex-wrap: wrap; gap: .35rem; margin-top: .55rem; }
.toolchip {
    display: inline-flex; gap: .35rem; align-items: baseline; max-width: 100%;
    padding: .2rem .5rem; border: 1px solid var(--border); border-radius: 7px;
    background: var(--surface); font-size: .76rem; color: var(--muted);
    font-family: ui-monospace, Menlo, Consolas, monospace;
}
.toolchip strong { color: var(--text); }
.toolchip span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chat-form { display: flex; gap: .7rem; align-items: flex-end; }
.chat-form .grow { flex: 1 1 auto; }
.chat-form textarea { min-height: 68px; resize: vertical; }
.chat-status { min-height: 1.4rem; margin: .1rem 0 .6rem; }
.chat-notice { font-size: .8rem; margin: .2rem 0 0; }
.chat-empty { text-align: center; margin: auto; }
.sr-only {
    position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px;
    overflow: hidden; clip: rect(0, 0, 0, 0); white-space: nowrap; border: 0;
}
@media (max-width: 720px) {
    .chat-form { flex-direction: column; align-items: stretch; }
    .chat-form .button { width: 100%; }
    .msg { max-width: 96%; }
    .session-list { position: static; width: 100%; margin-top: .5rem; }
}
