:root {
  --bg: #F7F4ED;
  --surface: #FFFFFF;
  --surface-subtle: #EFEAE0;
  --surface-hover: #F3EFE6;
  --ink: #16140F;
  --ink-2: #5D564B;
  --ink-3: #8A8175;
  --accent: #84552B;
  --accent-bg: rgba(132, 85, 43, 0.10);
  --accent-border: rgba(132, 85, 43, 0.28);
  --border: rgba(22, 20, 15, 0.09);
  --border-strong: rgba(22, 20, 15, 0.16);
  --green: #1E7A4C;
  --green-bg: rgba(30, 122, 76, 0.10);
  --amber: #A86416;
  --amber-bg: rgba(168, 100, 22, 0.11);
  --red: #B6382B;
  --red-bg: rgba(182, 56, 43, 0.10);
  --terminal-bg: #1B1915;
  --terminal-ink: #EFECE5;
  --radius: 14px;
  --radius-sm: 10px;
  --shadow-sm: 0 1px 2px rgba(22, 20, 15, 0.04);
  --shadow-sheet: 0 -8px 30px rgba(22, 20, 15, 0.14);
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --tab-h: 60px;
  --header-h: 54px;
  --font-sans: -apple-system, BlinkMacSystemFont, "SF Pro Text", "PingFang SC", "Hiragino Sans GB", "Noto Sans SC", sans-serif;
  --font-mono: ui-monospace, "SF Mono", Menlo, Monaco, Consolas, monospace;
}

* {
  box-sizing: border-box;
  -webkit-tap-highlight-color: transparent;
}

::selection {
  background: rgba(132, 85, 43, 0.2);
  color: var(--ink);
}

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--ink);
  font-family: var(--font-sans);
  font-size: 15px;
  line-height: 1.45;
  -webkit-font-smoothing: antialiased;
  overscroll-behavior-y: none;
}

button, input, textarea, select {
  font-family: inherit;
  font-size: inherit;
  color: inherit;
  caret-color: var(--accent);
}

button {
  cursor: pointer;
  border: none;
  background: none;
  padding: 0;
}

button:focus-visible, input:focus-visible, textarea:focus-visible, select:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

.num, .mono {
  font-variant-numeric: tabular-nums;
}

.mono {
  font-family: var(--font-mono);
}

/* Shell Layout */
.shell {
  min-height: 100dvh;
  max-width: 720px;
  margin: 0 auto;
  display: flex;
  flex-direction: column;
}

.topbar {
  position: sticky;
  top: 0;
  z-index: 20;
  background: rgba(247, 244, 237, 0.92);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  border-bottom: 1px solid var(--border);
  padding: calc(var(--safe-top) + 8px) 16px 10px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
}

.brand-wrap {
  display: flex;
  align-items: center;
  gap: 10px;
  min-width: 0;
}

.brand-mark {
  width: 26px;
  height: 26px;
  border-radius: 7px;
  background: var(--ink);
  color: var(--bg);
  display: grid;
  place-items: center;
  flex-shrink: 0;
}

.brand-title {
  font-size: 16px;
  font-weight: 700;
  letter-spacing: -0.02em;
}

.health-pills {
  display: flex;
  align-items: center;
  gap: 6px;
}

.health-dot-pill {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  font-size: 11px;
  font-weight: 600;
  color: var(--ink-2);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 3px 8px;
}

.dot {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: var(--ink-3);
  flex-shrink: 0;
}

.dot.ok { background: var(--green); }
.dot.warn { background: var(--amber); }
.dot.err { background: var(--red); }
.dot.pulse { animation: pulse 1.6s ease-in-out infinite; }

@keyframes pulse {
  0%, 100% { opacity: 1; transform: scale(1); }
  50% { opacity: 0.45; transform: scale(0.82); }
}

.top-actions {
  display: flex;
  align-items: center;
  gap: 6px;
}

.icon-btn {
  width: 34px;
  height: 34px;
  border-radius: 10px;
  background: var(--surface);
  border: 1px solid var(--border);
  display: grid;
  place-items: center;
  color: var(--ink);
  transition: background 0.15s ease, transform 0.1s ease;
}

.icon-btn:active {
  transform: scale(0.95);
  background: var(--surface-subtle);
}

.icon-btn.spinning svg {
  animation: spin 0.7s linear infinite;
}

@keyframes spin {
  to { transform: rotate(360deg); }
}

/* Main Content */
.main {
  flex: 1;
  padding: 14px 16px calc(var(--tab-h) + var(--safe-bottom) + 24px);
  display: flex;
  flex-direction: column;
  gap: 14px;
}

/* Section header */
.sec-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  margin-top: 4px;
}

.sec-title {
  font-size: 18px;
  font-weight: 700;
  letter-spacing: -0.02em;
  margin: 0;
}

.sec-sub {
  font-size: 12px;
  color: var(--ink-3);
}

/* Segmented control */
.segmented {
  display: flex;
  background: var(--surface-subtle);
  padding: 3px;
  border-radius: 11px;
  gap: 3px;
}

.seg-btn {
  flex: 1;
  padding: 7px 10px;
  border-radius: 8px;
  font-size: 13px;
  font-weight: 600;
  color: var(--ink-2);
  text-align: center;
  transition: background 0.15s ease, color 0.15s ease;
}

.seg-btn.active {
  background: var(--surface);
  color: var(--ink);
  box-shadow: var(--shadow-sm);
}

/* Surfaces & List Rows */
.panel {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
  box-shadow: var(--shadow-sm);
}

.panel-pad {
  padding: 14px;
}

.list-row {
  padding: 13px 14px;
  border-bottom: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.list-row:last-child {
  border-bottom: none;
}

.list-row.clickable {
  cursor: pointer;
  transition: background 0.12s ease;
}

.list-row.clickable:active {
  background: var(--surface-hover);
}

.row-top {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 10px;
}

.row-title {
  font-size: 15px;
  font-weight: 600;
  color: var(--ink);
  line-height: 1.35;
  word-break: break-word;
}

.row-desc {
  font-size: 13px;
  color: var(--ink-2);
  line-height: 1.45;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
  word-break: break-word;
}

.row-meta {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px;
  font-size: 12px;
  color: var(--ink-3);
}

.row-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-top: 4px;
}

/* Metric Grid (compact, factual, tabular) */
.metric-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 10px;
}

.metric-cell {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 12px 13px;
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.metric-label {
  font-size: 12px;
  color: var(--ink-2);
  font-weight: 500;
}

.metric-value {
  font-size: 22px;
  font-weight: 700;
  letter-spacing: -0.02em;
  color: var(--ink);
  font-variant-numeric: tabular-nums;
}

.metric-foot {
  font-size: 11px;
  color: var(--ink-3);
  font-variant-numeric: tabular-nums;
}

/* Badges */
.badge {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  font-size: 11px;
  font-weight: 600;
  padding: 2px 8px;
  border-radius: 999px;
  white-space: nowrap;
  flex-shrink: 0;
}

.badge.ok { background: var(--green-bg); color: var(--green); }
.badge.warn { background: var(--amber-bg); color: var(--amber); }
.badge.err { background: var(--red-bg); color: var(--red); }
.badge.muted { background: var(--surface-subtle); color: var(--ink-2); }
.badge.accent { background: var(--accent-bg); color: var(--accent); }

/* Buttons */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 9px 14px;
  border-radius: 10px;
  font-size: 13px;
  font-weight: 600;
  transition: opacity 0.15s ease, transform 0.1s ease, background 0.15s ease;
  white-space: nowrap;
}

.btn:active { transform: scale(0.97); }
.btn:disabled { opacity: 0.45; cursor: not-allowed; }

.btn-primary {
  background: var(--ink);
  color: var(--bg);
}

.btn-accent {
  background: var(--accent);
  color: #FFFFFF;
}

.btn-soft {
  background: var(--surface-subtle);
  color: var(--ink);
}

.btn-outline {
  background: var(--surface);
  color: var(--ink);
  border: 1px solid var(--border-strong);
}

.btn-danger {
  background: var(--red-bg);
  color: var(--red);
  border: 1px solid rgba(182, 56, 43, 0.24);
}

.btn-ok {
  background: var(--green-bg);
  color: var(--green);
  border: 1px solid rgba(30, 122, 76, 0.24);
}

.btn-sm {
  padding: 6px 10px;
  font-size: 12px;
  border-radius: 8px;
}

.btn-block {
  width: 100%;
}

/* Inputs */
.field {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.field label {
  font-size: 12px;
  font-weight: 600;
  color: var(--ink-2);
}

.input, .textarea, .select {
  width: 100%;
  background: var(--surface);
  border: 1px solid var(--border-strong);
  border-radius: 10px;
  padding: 10px 12px;
  font-size: 15px;
  color: var(--ink);
  appearance: none;
}

.textarea {
  resize: vertical;
  min-height: 86px;
  line-height: 1.5;
}

.quick-bar {
  display: flex;
  gap: 8px;
  align-items: center;
}

.quick-bar .input {
  flex: 1;
  background: var(--surface);
}

/* Terminal Log Box */
.terminal {
  background: var(--terminal-bg);
  color: var(--terminal-ink);
  border-radius: var(--radius-sm);
  padding: 12px;
  font-family: var(--font-mono);
  font-size: 11.5px;
  line-height: 1.55;
  overflow-x: auto;
  white-space: pre-wrap;
  word-break: break-all;
  max-height: 55vh;
  overflow-y: auto;
}

/* Notice / Alert banner */
.notice {
  background: var(--accent-bg);
  border: 1px solid var(--accent-border);
  border-radius: var(--radius-sm);
  padding: 11px 13px;
  font-size: 13px;
  color: var(--ink);
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 10px;
}

.notice.err {
  background: var(--red-bg);
  border-color: rgba(182, 56, 43, 0.25);
}

/* Empty & Loading */
.empty {
  padding: 36px 20px;
  text-align: center;
  color: var(--ink-3);
  font-size: 13px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 10px;
}

/* Chat Transcript */
.chat-list {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.chat-msg {
  padding: 11px 13px;
  border-radius: 12px;
  font-size: 14px;
  line-height: 1.55;
  white-space: pre-wrap;
  word-break: break-word;
}

.chat-msg.user {
  background: var(--ink);
  color: var(--bg);
  align-self: flex-end;
  max-width: 88%;
}

.chat-msg.assistant {
  background: var(--surface);
  border: 1px solid var(--border);
  color: var(--ink);
  align-self: flex-start;
  max-width: 96%;
}

.chat-time {
  font-size: 11px;
  opacity: 0.65;
  margin-top: 6px;
  font-variant-numeric: tabular-nums;
}

/* Todo row */
.todo-row {
  display: flex;
  align-items: flex-start;
  gap: 11px;
  padding: 13px 14px;
  border-bottom: 1px solid var(--border);
}

.todo-row:last-child { border-bottom: none; }

.todo-check {
  width: 22px;
  height: 22px;
  margin-top: 1px;
  border-radius: 6px;
  border: 1.75px solid var(--border-strong);
  background: var(--surface);
  display: grid;
  place-items: center;
  flex-shrink: 0;
  color: #fff;
}

.todo-check.checked {
  background: var(--green);
  border-color: var(--green);
}

.todo-body {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.todo-row.done .row-title {
  text-decoration: line-through;
  color: var(--ink-3);
}

/* Bottom TabBar */
.tabbar {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 30;
  background: rgba(255, 255, 255, 0.94);
  backdrop-filter: blur(14px);
  -webkit-backdrop-filter: blur(14px);
  border-top: 1px solid var(--border);
  padding: 6px 8px calc(6px + var(--safe-bottom));
}

.tabbar-inner {
  max-width: 720px;
  margin: 0 auto;
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 4px;
}

.tab-btn {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 3px;
  padding: 5px 4px;
  border-radius: 10px;
  color: var(--ink-3);
  font-size: 11px;
  font-weight: 600;
  position: relative;
  transition: color 0.15s ease, background 0.15s ease;
}

.tab-btn.active {
  color: var(--accent);
  background: var(--accent-bg);
}

.tab-badge {
  position: absolute;
  top: 2px;
  right: calc(50% - 20px);
  min-width: 16px;
  height: 16px;
  padding: 0 4px;
  border-radius: 999px;
  background: var(--accent);
  color: #fff;
  font-size: 10px;
  font-weight: 700;
  display: grid;
  place-items: center;
  font-variant-numeric: tabular-nums;
}

.tab-badge.err { background: var(--red); }

/* Bottom Sheet / Drawer */
.sheet-backdrop {
  position: fixed;
  inset: 0;
  z-index: 50;
  background: rgba(22, 20, 15, 0.42);
  display: flex;
  align-items: flex-end;
  justify-content: center;
  animation: fadeIn 0.16s ease-out;
}

.sheet {
  width: 100%;
  max-width: 720px;
  max-height: 90dvh;
  background: var(--bg);
  border-top-left-radius: 20px;
  border-top-right-radius: 20px;
  box-shadow: var(--shadow-sheet);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  animation: slideUp 0.22s cubic-bezier(0.16, 1, 0.3, 1);
}

@keyframes fadeIn { from { opacity: 0; } to { opacity: 1; } }
@keyframes slideUp { from { transform: translateY(32px); opacity: 0.7; } to { transform: translateY(0); opacity: 1; } }

.sheet-head {
  padding: 14px 16px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  flex-shrink: 0;
}

.sheet-title {
  font-size: 16px;
  font-weight: 700;
  margin: 0;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.sheet-body {
  padding: 16px;
  overflow-y: auto;
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.sheet-foot {
  padding: 12px 16px calc(12px + var(--safe-bottom));
  background: var(--surface);
  border-top: 1px solid var(--border);
  flex-shrink: 0;
}

/* Login Screen */
.login-wrap {
  min-height: 100dvh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px 16px;
}

.login-box {
  width: 100%;
  max-width: 360px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 18px;
  padding: 24px 20px;
  display: flex;
  flex-direction: column;
  gap: 16px;
  box-shadow: var(--shadow-sm);
}

.login-head {
  display: flex;
  align-items: center;
  gap: 12px;
}

.login-head h1 {
  margin: 0;
  font-size: 20px;
  letter-spacing: -0.02em;
}

.login-head p {
  margin: 2px 0 0;
  font-size: 13px;
  color: var(--ink-2);
}

/* Toast */
#toast-root {
  position: fixed;
  top: calc(var(--safe-top) + 60px);
  left: 50%;
  transform: translateX(-50%);
  z-index: 80;
  display: flex;
  flex-direction: column;
  gap: 8px;
  width: min(92vw, 380px);
  pointer-events: none;
}

.toast {
  background: var(--ink);
  color: var(--bg);
  padding: 10px 14px;
  border-radius: 12px;
  font-size: 13px;
  font-weight: 500;
  box-shadow: 0 6px 20px rgba(22, 20, 15, 0.22);
  animation: fadeIn 0.15s ease-out;
}

.toast.err {
  background: var(--red);
  color: #fff;
}

/* Custom thin scrollbars */
::-webkit-scrollbar { width: 5px; height: 5px; }
::-webkit-scrollbar-thumb { background: rgba(22, 20, 15, 0.18); border-radius: 99px; }
