:root {
  color-scheme: dark;
  font-family: Inter, "Segoe UI", "Microsoft YaHei UI", system-ui, sans-serif;
  --bg: #0f1015;
  --surface: #17181f;
  --surface-strong: #1d1e27;
  --surface-soft: #13141a;
  --text: #f4f1f4;
  --muted: #9c9aa5;
  --faint: #6f6d78;
  --border: #2b2c35;
  --border-strong: #3b3944;
  --primary: #b876a2;
  --primary-strong: #94587e;
  --primary-soft: #2b202b;
  --gold: #d0ab64;
  --success: #70bd92;
  --warning: #dcaf64;
  --danger: #d87580;
  --app-viewport-height: 100dvh;
  background: var(--bg);
  color: var(--text);
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { min-height: 100%; }
body { margin: 0; min-height: 100dvh; overflow-x: hidden; background: var(--bg); }
button, input, textarea, select { font: inherit; letter-spacing: 0; }
button { min-height: 44px; cursor: pointer; border: 1px solid var(--border); color: var(--text); background: var(--surface-strong); border-radius: 8px; padding: 9px 13px; transition: border-color 160ms ease, background 160ms ease, transform 120ms ease, color 160ms ease; }
button:active { transform: scale(.97); }
button:focus-visible, input:focus-visible, textarea:focus-visible, select:focus-visible { outline: 2px solid var(--gold); outline-offset: 2px; }
button:disabled { cursor: not-allowed; opacity: .5; }
svg { fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
h1, h2, p { margin: 0; }
h1 { font-size: 18px; line-height: 1.2; }
h2 { font-size: 18px; line-height: 1.25; }

.app-shell { width: min(840px, 100%); min-height: 100dvh; margin: 0 auto; background: var(--bg); }
.update-banner { position: fixed; top: max(10px, env(safe-area-inset-top)); left: 50%; z-index: 40; width: min(420px, calc(100% - 24px)); transform: translateX(-50%); display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 10px 12px; border: 1px solid var(--border-strong); border-radius: 8px; background: var(--surface-strong); box-shadow: 0 12px 30px #0008; }

.topbar { height: calc(64px + env(safe-area-inset-top)); display: flex; align-items: center; justify-content: space-between; gap: 14px; padding: env(safe-area-inset-top) 18px 0; border-bottom: 1px solid var(--border); background: #111217; }
.brand-lockup { display: flex; align-items: center; gap: 10px; min-width: 0; }
.brand-mark { width: 36px; height: 36px; flex: 0 0 auto; border: 1px solid #493a49; border-radius: 8px; }
.brand-lockup p { margin-top: 2px; color: var(--muted); font-size: 11px; }
.state { display: inline-flex; align-items: center; gap: 7px; flex: 0 0 auto; color: var(--muted); font-size: 12px; }
.state i { width: 7px; height: 7px; border-radius: 50%; background: var(--faint); box-shadow: 0 0 0 3px #6f6d7820; }
.state.connected { color: var(--success); }
.state.connected i { background: var(--success); box-shadow: 0 0 0 3px #70bd9220; }
.state.offline { color: var(--warning); }
.state.offline i { background: var(--warning); box-shadow: 0 0 0 3px #dcaf6420; }

.login-panel { min-height: calc(100dvh - 64px - env(safe-area-inset-top)); display: grid; align-content: center; gap: 28px; padding: 32px 22px max(32px, env(safe-area-inset-bottom)); }
.login-intro, .login-fields { width: min(420px, 100%); margin: 0 auto; }
.login-kicker, .page-heading span, .section-kicker, .more-sheet header span { display: block; color: var(--gold); font-size: 10px; font-weight: 700; letter-spacing: 0; }
.login-intro h2 { margin-top: 7px; font-size: 28px; }
.login-intro p { margin-top: 10px; max-width: 340px; }
.login-fields { display: grid; gap: 14px; padding: 18px; border: 1px solid var(--border); border-radius: 8px; background: var(--surface); box-shadow: 0 14px 40px #0004; }
label { display: grid; gap: 7px; color: #c2bec7; font-size: 13px; }
input, textarea, select { width: 100%; border: 1px solid var(--border-strong); border-radius: 8px; background: #0d0e13; color: var(--text); padding: 11px 12px; outline: none; }
input::placeholder, textarea::placeholder { color: #77747f; }
input:focus, textarea:focus, select:focus { border-color: var(--primary); }
textarea { resize: none; }
.primary { border-color: #c383ad; background: var(--primary-strong); color: #fff; }
.primary:active { background: #824c70; }
.login-button { width: 100%; margin-top: 2px; }
.muted, .empty { color: var(--muted); font-size: 13px; line-height: 1.55; }
.error { min-height: 20px; color: #f39ca4; font-size: 13px; }

.main-panel { min-height: calc(100dvh - 64px - env(safe-area-inset-top)); }
.page-heading { height: 58px; display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 8px 18px; border-bottom: 1px solid var(--border); background: var(--bg); }
.page-heading h2 { margin-top: 2px; }
.page-heading .privacy-mark { display: inline-flex; align-items: center; height: 24px; padding: 0 8px; border: 1px solid #4d4131; border-radius: 5px; color: var(--gold); background: #201b14; font-size: 9px; }
.content-viewport { min-height: calc(100dvh - 64px - 58px - 68px - env(safe-area-inset-top) - env(safe-area-inset-bottom)); }
.tab-content { min-height: calc(100dvh - 64px - 58px - 68px - env(safe-area-inset-top) - env(safe-area-inset-bottom)); padding: 16px 16px calc(92px + env(safe-area-inset-bottom)); }

.bottom-nav { position: fixed; left: 50%; bottom: 0; z-index: 20; width: min(840px, 100%); height: calc(68px + env(safe-area-inset-bottom)); transform: translateX(-50%); display: grid; grid-template-columns: repeat(5, 1fr); padding: 5px 8px env(safe-area-inset-bottom); border-top: 1px solid var(--border); background: #121318f5; backdrop-filter: blur(16px); }
.bottom-nav button { position: relative; min-width: 0; height: 58px; display: grid; place-items: center; align-content: center; gap: 3px; border: 0; border-radius: 6px; padding: 3px; background: transparent; color: var(--muted); font-size: 10px; }
.bottom-nav button svg { width: 21px; height: 21px; }
.bottom-nav button.active { color: var(--primary); background: #b876a20d; }
.badge { position: absolute; top: 3px; left: calc(50% + 6px); min-width: 17px; height: 17px; display: grid; place-items: center; padding: 0 4px; border: 2px solid #121318; border-radius: 9px; background: var(--danger); color: white; font-size: 9px; line-height: 1; }

.section-heading { min-height: 48px; display: flex; align-items: center; justify-content: space-between; gap: 14px; margin-bottom: 14px; }
.section-heading h2 { margin-top: 3px; font-size: 21px; }
.section-heading button, .refresh-button { min-height: 38px; padding: 7px 11px; color: #c8c4cd; font-size: 12px; }
.list { display: grid; gap: 9px; }
.list-item { position: relative; overflow: hidden; border: 1px solid var(--border); border-radius: 8px; padding: 14px; background: var(--surface); box-shadow: 0 5px 18px #0002; }
.list-item.unread { border-left: 3px solid var(--primary); }
.list-item strong { display: block; margin-bottom: 6px; color: var(--text); font-size: 14px; line-height: 1.4; overflow-wrap: anywhere; }
.list-item small { display: block; color: #bbb7c0; line-height: 1.55; overflow-wrap: anywhere; }
.list-item > .muted { margin-top: 8px; color: var(--faint); font-size: 11px; }
.item-actions { display: flex; flex-wrap: wrap; gap: 7px; margin-top: 11px; }
.item-actions button { min-height: 38px; font-size: 12px; }
.heading-actions { margin-top: 0; }
.empty { min-height: 130px; display: grid; place-items: center; padding: 24px; border: 1px dashed var(--border-strong); border-radius: 8px; text-align: center; background: #13141a80; }

.chat-content { height: calc(var(--app-viewport-height) - 64px - 58px - 68px - env(safe-area-inset-top) - env(safe-area-inset-bottom)); min-height: 360px; display: grid; grid-template-rows: minmax(0, 1fr) auto auto; padding: 0; overflow: hidden; }
.messages { min-height: 0; display: flex; flex-direction: column; gap: 12px; overflow-y: auto; overscroll-behavior: contain; padding: 18px 16px 12px; scroll-behavior: smooth; }
.message { width: 100%; display: flex; align-items: flex-end; gap: 8px; color: var(--text); font-size: 14px; line-height: 1.55; }
.message.owner { justify-content: flex-end; }
.message-avatar { width: 30px; height: 30px; flex: 0 0 auto; overflow: hidden; border: 1px solid #493a49; border-radius: 8px; background: var(--surface-strong); }
.message-avatar img { width: 100%; height: 100%; display: block; }
.message-bubble { width: fit-content; max-width: min(78%, 560px); padding: 10px 12px; border: 1px solid var(--border); border-radius: 8px 8px 8px 2px; background: var(--surface-strong); white-space: pre-wrap; overflow-wrap: anywhere; }
.message.owner .message-bubble { border-color: #6e4b65; border-radius: 8px 8px 2px 8px; background: #50364a; }
.message.pending { opacity: .7; }
.message-meta { min-height: 18px; display: flex; align-items: center; gap: 8px; margin-top: 5px; color: #a8a4ae; font-size: 10px; }
.play { min-height: 28px; padding: 2px 8px; border-color: #55515d; background: transparent; font-size: 10px; }
.outbox-status { min-height: 18px; padding: 0 16px 4px; }
.composer { display: grid; grid-template-columns: minmax(0, 1fr) 46px; gap: 8px; align-items: end; padding: 10px 12px 12px; border-top: 1px solid var(--border); background: #121318; }
.composer textarea { min-height: 46px; max-height: 120px; padding: 12px 13px; line-height: 20px; }
.send-button { width: 46px; height: 46px; min-height: 46px; display: grid; place-items: center; padding: 0; border-radius: 8px; }
.send-button svg { width: 20px; height: 20px; }

.keyboard-open .bottom-nav { display: none; }
.keyboard-open .chat-content {
  height: calc(var(--app-viewport-height) - 64px - 58px - env(safe-area-inset-top));
  min-height: 0;
}
.keyboard-open .composer { padding-bottom: max(8px, env(safe-area-inset-bottom)); }

.inline-form { display: grid; grid-template-columns: minmax(0, 1fr) minmax(160px, 210px) auto; gap: 8px; margin-bottom: 14px; padding: 12px; border: 1px solid var(--border); border-radius: 8px; background: var(--surface-soft); }
.capture-form { grid-template-columns: minmax(0, 1fr) auto; }
.memory-filters { display: grid; grid-template-columns: minmax(0, 1fr) 150px; gap: 8px; margin-bottom: 8px; }
#memoryCount { margin: 0 2px 12px; }
.summary-strip { margin-bottom: 12px; padding: 10px 12px; border-left: 2px solid var(--gold); background: #1d1913; }
.task-item summary { min-height: 28px; display: flex; align-items: center; justify-content: space-between; gap: 10px; cursor: pointer; list-style: none; }
.task-item summary::-webkit-details-marker { display: none; }
.task-item summary strong { margin: 0; min-width: 0; }
.task-status { flex: 0 0 auto; padding: 4px 7px; border: 1px solid var(--border-strong); border-radius: 5px; color: var(--muted); background: var(--surface-soft); font-size: 10px; }
.task-status.running { border-color: #375c47; color: var(--success); background: #17241d; }
.task-status.waiting_for_user { border-color: #685435; color: var(--warning); background: #251f16; }
.task-detail { display: grid; gap: 12px; margin-top: 14px; padding-top: 13px; border-top: 1px solid var(--border); }
.task-detail p { margin-top: 3px; line-height: 1.55; overflow-wrap: anywhere; }
.task-continuation-form { display: grid; grid-template-columns: minmax(0, 1fr) auto; gap: 8px; align-items: end; padding-top: 4px; }
.task-continuation-form p { grid-column: 1 / -1; min-height: 20px; }
.task-control-actions { display: grid; gap: 6px; }
button.danger { border-color: #70424a; color: #ffd6da; background: #321d22; }
.file-preview { max-height: 48vh; overflow: auto; margin-top: 12px; white-space: pre-wrap; overflow-wrap: anywhere; border: 1px solid var(--border); background: #0d0e13; border-radius: 8px; padding: 14px; color: #dad7df; font: 12px/1.65 "Cascadia Code", Consolas, monospace; }
.subheading { margin: 22px 0 11px; font-size: 15px; }
.settings-actions { display: flex; justify-content: flex-end; margin-top: 20px; }
.notification-form { display: grid; gap: 14px; padding: 14px; border: 1px solid var(--border); border-radius: 8px; background: var(--surface); }
.notification-form fieldset { min-width: 0; margin: 0; padding: 11px; border: 1px solid var(--border); border-radius: 6px; }
.notification-form legend { padding: 0 6px; color: var(--muted); font-size: 12px; }
.toggle-row { display: flex; align-items: center; justify-content: space-between; gap: 12px; }
.toggle-row input { width: 22px; height: 22px; }
.category-grid { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 11px; }
.category-grid label { display: flex; align-items: center; gap: 7px; min-width: 0; }
.category-grid input { width: 18px; height: 18px; flex: 0 0 auto; accent-color: var(--primary); }

.sheet-backdrop { position: fixed; inset: 0; z-index: 29; background: #0009; animation: fade-in 180ms ease-out; }
.more-sheet { position: fixed; left: 50%; bottom: 0; z-index: 30; width: min(840px, 100%); transform: translateX(-50%); padding: 18px 16px max(22px, env(safe-area-inset-bottom)); border-top: 1px solid var(--border-strong); border-radius: 8px 8px 0 0; background: #17181f; box-shadow: 0 -18px 45px #0009; animation: sheet-in 220ms ease-out; }
.more-sheet header { display: flex; align-items: center; justify-content: space-between; margin-bottom: 14px; }
.more-sheet header h2 { margin-top: 3px; }
.icon-button { width: 44px; height: 44px; display: grid; place-items: center; padding: 0; }
.icon-button svg { width: 20px; height: 20px; }
.more-grid { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 8px; }
.more-grid button { min-height: 104px; display: grid; align-content: center; justify-items: start; gap: 5px; padding: 13px; text-align: left; background: #111218; }
.more-grid button svg { width: 22px; height: 22px; margin-bottom: 5px; color: var(--primary); }
.more-grid button span { font-size: 13px; font-weight: 700; }
.more-grid button small { color: var(--muted); font-size: 10px; }
.more-grid button.active { border-color: #74516a; background: var(--primary-soft); }

@keyframes fade-in { from { opacity: 0; } to { opacity: 1; } }
@keyframes sheet-in { from { transform: translate(-50%, 24px); opacity: 0; } to { transform: translate(-50%, 0); opacity: 1; } }

@media (min-width: 700px) {
  body { background: #090a0e; }
  .app-shell { border-inline: 1px solid #202129; box-shadow: 0 0 60px #0008; }
  .tab-content:not(.chat-content) { padding-inline: 24px; }
}

@media (max-width: 560px) {
  .inline-form, .memory-filters, .task-continuation-form { grid-template-columns: 1fr; }
  .task-continuation-form button { width: 100%; }
  .category-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .more-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .more-grid button { min-height: 94px; }
  .message-bubble { max-width: 82%; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { scroll-behavior: auto !important; animation-duration: .01ms !important; transition-duration: .01ms !important; }
}
