:root {
  --bg: #f6f5f1;
  --surface: #ffffff;
  --surface-2: #f0eee8;
  --border: #e3e0d7;
  --text: #1d1c1a;
  --muted: #6b6860;
  --accent: #2f5d50;
  --accent-soft: #e2ece8;
  --accent-text: #ffffff;
  --warn: #a5561a;
  --warn-soft: #f8eadf;
  --danger: #a83232;
  --danger-soft: #f7e3e3;
  --ok: #2e7d4f;
  --ok-soft: #e1f1e7;
  --user-bubble: #2f5d50;
  --user-text: #ffffff;
  --shadow: 0 1px 2px rgba(0,0,0,.04), 0 4px 16px rgba(0,0,0,.04);
  --radius: 12px;
  --font: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  --serif: "Iowan Old Style", "Palatino Linotype", Palatino, Georgia, serif;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #161715;
    --surface: #1f201e;
    --surface-2: #272926;
    --border: #353733;
    --text: #ecebe6;
    --muted: #a3a199;
    --accent: #7fb8a6;
    --accent-soft: #23332e;
    --accent-text: #0f1a16;
    --warn: #e0a06a;
    --warn-soft: #3a2a1d;
    --danger: #e08585;
    --danger-soft: #3a2222;
    --ok: #7cc79a;
    --ok-soft: #1f3327;
    --user-bubble: #2c4a41;
    --user-text: #ecebe6;
    --shadow: none;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body { background: var(--bg); color: var(--text); font: 15px/1.55 var(--font); -webkit-font-smoothing: antialiased; }
a { color: var(--accent); }
button, input, select, textarea { font: inherit; color: inherit; }
h1, h2, h3 { line-height: 1.25; margin: 0 0 .5em; }
h1 { font-size: 1.5rem; font-weight: 650; letter-spacing: -.01em; }
h2 { font-size: 1.1rem; font-weight: 650; }
h3 { font-size: .95rem; font-weight: 650; }
.muted { color: var(--muted); }
.small { font-size: .85rem; }
.hidden { display: none !important; }

/* ---------- layout ---------- */
.app { display: grid; grid-template-columns: 228px 1fr; height: 100vh; height: 100dvh; }
.sidebar { background: var(--surface); border-right: 1px solid var(--border); display: flex; flex-direction: column; padding: 18px 12px; gap: 2px; overflow-y: auto; }
.brand { display: flex; align-items: center; gap: 10px; padding: 4px 10px 18px; font-weight: 700; font-size: 1.05rem; }
.brand .logo { width: 30px; height: 30px; border-radius: 8px; background: var(--accent); color: var(--accent-text); display: grid; place-items: center; font-family: var(--serif); font-size: 1.1rem; }
.nav a { display: flex; align-items: center; gap: 10px; padding: 9px 12px; border-radius: 9px; color: var(--text); text-decoration: none; font-weight: 500; }
.nav a:hover { background: var(--surface-2); }
.nav a.active { background: var(--accent-soft); color: var(--accent); }
.nav svg { width: 18px; height: 18px; flex: none; opacity: .85; }
.sidebar .spacer { flex: 1; }
.sidebar .foot { padding: 10px 12px; font-size: .8rem; color: var(--muted); }
.sidebar .foot button { background: none; border: 0; color: var(--muted); padding: 0; cursor: pointer; text-decoration: underline; }
.main { overflow-y: auto; min-width: 0; }
.page { max-width: 980px; margin: 0 auto; padding: 28px 28px 80px; }
.page-head { display: flex; align-items: flex-end; justify-content: space-between; gap: 16px; margin-bottom: 18px; flex-wrap: wrap; }
.page-head p { margin: 2px 0 0; }
.topbar { display: none; }

@media (max-width: 820px) {
  .app { grid-template-columns: 1fr; grid-template-rows: auto 1fr; }
  .sidebar { position: fixed; inset: 0 30% 0 0; z-index: 30; transform: translateX(-100%); transition: transform .2s; box-shadow: 0 0 40px rgba(0,0,0,.2); }
  .sidebar.open { transform: none; }
  .scrim { position: fixed; inset: 0; background: rgba(0,0,0,.3); z-index: 20; }
  .topbar { display: flex; align-items: center; gap: 10px; padding: 10px 16px; background: var(--surface); border-bottom: 1px solid var(--border); }
  .topbar button { background: none; border: 0; padding: 4px; cursor: pointer; }
  .topbar .title { font-weight: 650; }
  .page { padding: 18px 16px 80px; }
}

/* ---------- components ---------- */
.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 18px; box-shadow: var(--shadow); }
.card + .card { margin-top: 14px; }
.grid > .card { margin-top: 0; }
.grid { display: grid; gap: 14px; }
.grid.two { grid-template-columns: 1fr 1fr; }
.grid.three { grid-template-columns: repeat(3, 1fr); }
@media (max-width: 820px) { .grid.two, .grid.three { grid-template-columns: 1fr; } }
.row { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; }
.row.between { justify-content: space-between; }
.stack > * + * { margin-top: 10px; }

.btn { display: inline-flex; align-items: center; gap: 6px; border: 1px solid var(--border); background: var(--surface); padding: 8px 14px; border-radius: 9px; cursor: pointer; font-weight: 550; white-space: nowrap; text-decoration: none; }
.btn:hover { background: var(--surface-2); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }
.btn.primary:hover { filter: brightness(1.08); }
.btn.small { padding: 5px 10px; font-size: .85rem; }
.btn.ghost { border-color: transparent; background: none; }
.btn.danger { color: var(--danger); }
.btn:disabled { opacity: .55; cursor: default; }
.iconbtn { border: 0; background: none; cursor: pointer; color: var(--muted); padding: 4px 6px; border-radius: 6px; }
.iconbtn:hover { background: var(--surface-2); color: var(--text); }

input[type=text], input[type=password], input[type=url], input[type=date], input[type=number], select, textarea {
  width: 100%; padding: 9px 11px; border: 1px solid var(--border); border-radius: 9px; background: var(--surface); outline: none;
}
input:focus, select:focus, textarea:focus { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
textarea { resize: vertical; min-height: 70px; }
label { display: block; font-size: .82rem; font-weight: 600; color: var(--muted); margin-bottom: 4px; }
.field { margin-bottom: 10px; }
.inline-form { display: grid; gap: 8px; grid-template-columns: 2fr 1fr 1fr auto; align-items: end; }
.inline-form label { margin-bottom: 4px; }
@media (max-width: 820px) { .inline-form { grid-template-columns: 1fr 1fr; } .inline-form > :first-child { grid-column: 1 / -1; } }

.badge { display: inline-block; padding: 2px 8px; border-radius: 999px; font-size: .75rem; font-weight: 600; background: var(--surface-2); color: var(--muted); white-space: nowrap; }
.badge.ok { background: var(--ok-soft); color: var(--ok); }
.badge.warn { background: var(--warn-soft); color: var(--warn); }
.badge.danger { background: var(--danger-soft); color: var(--danger); }
.badge.accent { background: var(--accent-soft); color: var(--accent); }

.empty { text-align: center; padding: 34px 16px; color: var(--muted); }
.banner { padding: 12px 16px; border-radius: var(--radius); background: var(--warn-soft); color: var(--warn); margin-bottom: 16px; }
.toast { position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%); background: var(--text); color: var(--bg); padding: 10px 18px; border-radius: 10px; z-index: 50; font-weight: 500; max-width: 90vw; }

.list { list-style: none; padding: 0; margin: 0; }
.list li { display: flex; gap: 10px; align-items: flex-start; padding: 10px 0; border-bottom: 1px solid var(--border); }
.list li:last-child { border-bottom: 0; }
.list .grow { flex: 1; min-width: 0; }
.days { font-variant-numeric: tabular-nums; font-weight: 650; min-width: 52px; }
.days.soon { color: var(--danger); }
.done-text { text-decoration: line-through; color: var(--muted); }

.progress { height: 8px; background: var(--surface-2); border-radius: 99px; overflow: hidden; }
.progress > div { height: 100%; background: var(--accent); border-radius: 99px; transition: width .3s; }
.chips { display: flex; flex-wrap: wrap; gap: 6px; }
.chip { padding: 4px 10px; border-radius: 999px; border: 1px solid var(--border); font-size: .8rem; }
.chip.done { background: var(--ok-soft); border-color: transparent; color: var(--ok); }
.stat { font-size: 1.8rem; font-weight: 700; line-height: 1.1; }

table.table { width: 100%; border-collapse: collapse; }
.table th, .table td { text-align: left; padding: 10px 8px; border-bottom: 1px solid var(--border); vertical-align: top; font-size: .92rem; }
.table th { font-size: .75rem; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); font-weight: 650; }
.table select { padding: 4px 6px; font-size: .85rem; width: auto; }

/* ---------- markdown ---------- */
.md > :first-child { margin-top: 0; }
.md > :last-child { margin-bottom: 0; }
.md p { margin: 0 0 .7em; }
.md ul, .md ol { margin: 0 0 .7em; padding-left: 1.3em; }
.md li { margin: .2em 0; }
.md table { border-collapse: collapse; margin: .5em 0 .9em; font-size: .9rem; display: block; overflow-x: auto; }
.md th, .md td { border: 1px solid var(--border); padding: 6px 9px; text-align: left; }
.md code { background: var(--surface-2); padding: 1px 5px; border-radius: 4px; font-size: .88em; }
.md blockquote { margin: .5em 0; padding-left: 12px; border-left: 3px solid var(--border); color: var(--muted); }
.md h1, .md h2, .md h3 { margin: 1em 0 .4em; }

/* ---------- chat ---------- */
.chat { display: flex; flex-direction: column; height: 100%; }
.chat-scroll { flex: 1; overflow-y: auto; }
.chat-inner { max-width: 760px; margin: 0 auto; padding: 24px 20px 12px; }
.msg { margin-bottom: 18px; display: flex; }
.msg.user { justify-content: flex-end; }
.msg .bubble { max-width: 85%; }
.msg.user .bubble { background: var(--user-bubble); color: var(--user-text); padding: 10px 14px; border-radius: 16px 16px 4px 16px; white-space: pre-wrap; word-wrap: break-word; }
.msg.assistant .bubble { max-width: 100%; }
.msg.assistant .who { font-size: .78rem; font-weight: 650; color: var(--muted); margin-bottom: 4px; display: flex; align-items: center; gap: 6px; }
.msg.assistant .who .dot { width: 18px; height: 18px; border-radius: 5px; background: var(--accent); color: var(--accent-text); display: grid; place-items: center; font-family: var(--serif); font-size: .7rem; }
.msg .attach { font-size: .8rem; opacity: .85; margin-top: 4px; }
.steps { margin: 4px 0 8px; display: flex; flex-direction: column; gap: 4px; }
.step { font-size: .83rem; color: var(--muted); display: flex; gap: 6px; align-items: center; }
.step .spin { width: 12px; height: 12px; border: 2px solid var(--border); border-top-color: var(--accent); border-radius: 50%; animation: spin .8s linear infinite; }
.step .check { color: var(--ok); }
@keyframes spin { to { transform: rotate(360deg); } }
.typing { display: inline-flex; gap: 4px; padding: 6px 0; }
.typing i { width: 6px; height: 6px; border-radius: 50%; background: var(--muted); animation: blink 1.2s infinite; }
.typing i:nth-child(2) { animation-delay: .2s; } .typing i:nth-child(3) { animation-delay: .4s; }
@keyframes blink { 0%, 80%, 100% { opacity: .25; } 40% { opacity: 1; } }
.composer { border-top: 1px solid var(--border); background: var(--surface); padding: 12px 16px; }
.composer-inner { max-width: 760px; margin: 0 auto; display: flex; gap: 8px; align-items: flex-end; }
.composer textarea { min-height: 44px; max-height: 220px; resize: none; border-radius: 12px; }
.composer .attach-name { max-width: 760px; margin: 0 auto 6px; font-size: .82rem; color: var(--muted); }
.load-earlier { display: block; margin: 0 auto 16px; }

/* ---------- essays ---------- */
.editor textarea { min-height: 420px; font-family: var(--serif); font-size: 1.05rem; line-height: 1.7; padding: 16px 18px; }
.wordbar { display: flex; justify-content: space-between; align-items: center; font-size: .85rem; color: var(--muted); margin-top: 6px; gap: 10px; flex-wrap: wrap; }
.over { color: var(--danger); font-weight: 650; }
.prompt-box { background: var(--surface-2); border-radius: 10px; padding: 12px 14px; font-size: .92rem; margin-bottom: 12px; white-space: pre-wrap; }
.review h3 { margin-top: 14px; }
.review .fix { border-left: 3px solid var(--warn); padding: 4px 0 4px 12px; margin: 10px 0; }
.review .quote { font-family: var(--serif); font-style: italic; color: var(--muted); }
.verdict { font-size: .85rem; }
.counter { font-size: .75rem; color: var(--muted); text-align: right; margin-top: 2px; }
.counter.over { color: var(--danger); }
.item-card { border: 1px solid var(--border); border-radius: var(--radius); padding: 14px; background: var(--surface); margin-bottom: 12px; }
.item-card .grid { gap: 8px; }
.item-head { display: flex; justify-content: space-between; align-items: center; margin-bottom: 8px; }
.pos { width: 26px; height: 26px; border-radius: 50%; background: var(--accent-soft); color: var(--accent); display: grid; place-items: center; font-weight: 700; font-size: .85rem; }

.tabs { display: flex; gap: 4px; border-bottom: 1px solid var(--border); margin-bottom: 14px; }
.tabs button { background: none; border: 0; padding: 8px 12px; cursor: pointer; border-bottom: 2px solid transparent; color: var(--muted); font-weight: 600; }
.tabs button.active { color: var(--accent); border-bottom-color: var(--accent); }

.profile-field { padding: 8px 0; border-bottom: 1px solid var(--border); }
.profile-field:last-child { border-bottom: 0; }
.profile-field .value { white-space: pre-wrap; cursor: text; min-height: 1.5em; border-radius: 6px; padding: 2px 4px; margin: 0 -4px; }
.profile-field .value:hover { background: var(--surface-2); }
.profile-field .value.unknown { color: var(--muted); font-style: italic; }

.login-wrap { min-height: 100vh; display: grid; place-items: center; padding: 16px; }
.login-card { width: 100%; max-width: 360px; }
