:root {
  --bg: #f6f4ef;
  --bg-deep: #ebe7df;
  --card: #ffffff;
  --ink: #1c1b18;
  --ink-2: #5c574e;
  --ink-3: #8a8478;
  --line: #e4dfd5;
  --accent: #2f6f63;
  --accent-soft: #dceee9;
  --accent-ink: #ffffff;
  --danger: #b3423a;
  --user: #2f6f63;
  --bot-bg: #ffffff;
  --shadow: 0 10px 30px rgba(28, 27, 24, 0.06);
  --radius: 16px;
  --tabbar-h: 64px;
  --topbar-h: 58px;
  font-family: "PingFang SC", "Microsoft YaHei", "Noto Sans SC", -apple-system, sans-serif;
}

* { box-sizing: border-box; }
html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--ink);
  -webkit-tap-highlight-color: transparent;
}
button, input, textarea, select {
  font: inherit;
  color: inherit;
}
.hidden { display: none !important; }

/* ===== Login ===== */
.login-wrap {
  min-height: 100%;
  display: grid;
  place-items: center;
  padding: 24px;
  background:
    radial-gradient(1200px 500px at 10% -10%, #e7f2ee 0%, transparent 50%),
    radial-gradient(900px 400px at 100% 0%, #f0e8d8 0%, transparent 45%),
    var(--bg);
}
.login-card {
  width: min(420px, 100%);
  background: var(--card);
  border-radius: 24px;
  padding: 28px 24px 24px;
  box-shadow: var(--shadow);
  border: 1px solid var(--line);
}
.brand { text-align: center; margin-bottom: 22px; }
.brand h1 {
  margin: 12px 0 6px;
  font-size: 24px;
  letter-spacing: 0.02em;
}
.brand p {
  margin: 0;
  color: var(--ink-2);
  font-size: 14px;
}
.logo {
  width: 56px;
  height: 56px;
  margin: 0 auto;
  border-radius: 18px;
  display: grid;
  place-items: center;
  background: var(--accent);
  color: white;
  font-weight: 700;
  font-size: 22px;
  box-shadow: 0 8px 20px rgba(47, 111, 99, 0.28);
}
.logo.sm {
  width: 36px;
  height: 36px;
  border-radius: 12px;
  font-size: 15px;
  margin: 0;
  box-shadow: none;
}
#login-form label {
  display: block;
  font-size: 13px;
  color: var(--ink-2);
  margin-bottom: 8px;
}
#login-form input {
  width: 100%;
  border: 1px solid var(--line);
  background: var(--bg);
  border-radius: 12px;
  padding: 12px 14px;
  outline: none;
}
#login-form input:focus {
  border-color: var(--accent);
  background: #fff;
}
.btn-primary {
  border: 0;
  background: var(--accent);
  color: var(--accent-ink);
  border-radius: 12px;
  padding: 12px 16px;
  font-weight: 600;
  cursor: pointer;
}
.btn-primary:active { transform: scale(0.98); }
#login-form .btn-primary {
  width: 100%;
  margin-top: 14px;
}
.error {
  color: var(--danger);
  min-height: 1.2em;
  font-size: 13px;
  margin: 10px 0 0;
}
.hint {
  color: var(--ink-3);
  font-size: 12px;
  margin: 8px 4px 0;
  line-height: 1.5;
}

/* ===== App shell ===== */
.app {
  min-height: 100%;
  display: flex;
  flex-direction: column;
  max-width: 820px;
  margin: 0 auto;
}
.topbar {
  height: var(--topbar-h);
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 14px;
  background: rgba(246, 244, 239, 0.9);
  backdrop-filter: blur(10px);
  border-bottom: 1px solid var(--line);
  position: sticky;
  top: 0;
  z-index: 20;
}
.topbar-left {
  display: flex;
  gap: 10px;
  align-items: center;
}
.title { font-weight: 700; font-size: 16px; }
.subtitle { color: var(--ink-3); font-size: 12px; }
.icon-btn {
  width: 40px;
  height: 40px;
  border-radius: 12px;
  border: 1px solid var(--line);
  background: var(--card);
  font-size: 22px;
  line-height: 1;
  cursor: pointer;
}
.ghost-btn {
  border: 1px solid var(--line);
  background: transparent;
  color: var(--ink-2);
  border-radius: 10px;
  padding: 8px 12px;
  font-size: 13px;
  cursor: pointer;
}
.row { display: flex; gap: 8px; align-items: center; }

#main {
  flex: 1;
  padding: 12px 12px calc(var(--tabbar-h) + 16px);
}
.view { display: none; }
.view.active { display: block; }

.page-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin: 4px 2px 12px;
}
.page-head h2 {
  margin: 0;
  font-size: 20px;
}

/* ===== Chat ===== */
.session-bar {
  display: flex;
  gap: 8px;
  margin-bottom: 10px;
}
.session-bar select {
  flex: 1;
  border: 1px solid var(--line);
  background: var(--card);
  border-radius: 12px;
  padding: 10px 12px;
}
.messages {
  display: flex;
  flex-direction: column;
  gap: 12px;
  padding-bottom: 8px;
  min-height: 42vh;
}
.msg {
  max-width: 92%;
  padding: 12px 14px;
  border-radius: 16px;
  line-height: 1.65;
  font-size: 15px;
  white-space: pre-wrap;
  word-break: break-word;
  border: 1px solid transparent;
  animation: fadein 0.18s ease;
}
@keyframes fadein {
  from { opacity: 0; transform: translateY(4px); }
  to { opacity: 1; transform: none; }
}
.msg.user {
  align-self: flex-end;
  background: var(--user);
  color: #fff;
  border-bottom-right-radius: 6px;
}
.msg.assistant {
  align-self: flex-start;
  background: var(--bot-bg);
  border-color: var(--line);
  border-bottom-left-radius: 6px;
  box-shadow: var(--shadow);
}
.msg .meta {
  margin-top: 8px;
  font-size: 11px;
  color: var(--ink-3);
}
.msg.user .meta { color: rgba(255,255,255,0.75); }

.tools {
  margin-top: 10px;
  display: flex;
  flex-direction: column;
  gap: 6px;
}
.tool {
  background: var(--bg);
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 8px 10px;
  font-size: 12px;
  color: var(--ink-2);
}
.tool .name {
  font-weight: 600;
  color: var(--accent);
}
.tool .detail {
  margin-top: 3px;
  opacity: 0.85;
  word-break: break-all;
}

.empty {
  text-align: center;
  color: var(--ink-3);
  padding: 48px 12px;
  line-height: 1.8;
  font-size: 14px;
}

.composer {
  position: sticky;
  bottom: calc(var(--tabbar-h) + 8px);
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 8px;
  padding: 8px 0 4px;
  background: linear-gradient(to top, var(--bg) 70%, transparent);
}
.composer textarea {
  resize: none;
  border: 1px solid var(--line);
  background: var(--card);
  border-radius: 14px;
  padding: 12px 14px;
  outline: none;
  max-height: 140px;
  line-height: 1.5;
}
.composer textarea:focus { border-color: var(--accent); }
.send { align-self: end; min-width: 84px; }
.send:disabled {
  opacity: 0.55;
  cursor: not-allowed;
}

/* ===== Tasks / Status ===== */
.card {
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 14px;
  box-shadow: var(--shadow);
}
.cards {
  display: grid;
  gap: 12px;
}
.card h3 {
  margin: 0 0 8px;
  font-size: 15px;
}
.card pre {
  margin: 0;
  white-space: pre-wrap;
  word-break: break-word;
  font-size: 12px;
  line-height: 1.6;
  color: var(--ink-2);
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
}
.stack pre + pre {
  margin-top: 10px;
  padding-top: 10px;
  border-top: 1px dashed var(--line);
}
.status-line {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  padding: 8px 0;
  border-bottom: 1px dashed var(--line);
  font-size: 14px;
  align-items: center;
}
.status-line .k {
  color: var(--ink-2);
  flex: 1;
}
.status-line .v {
  font-weight: 600;
  text-align: right;
  max-width: 58%;
  word-break: break-all;
}
.meter {
  height: 8px;
  background: var(--bg-deep);
  border-radius: 999px;
  overflow: hidden;
  margin-top: 6px;
}
.meter > i {
  display: block;
  height: 100%;
  background: var(--accent);
  border-radius: 999px;
}
.meter.warn > i { background: #c4842d; }
.meter.bad > i { background: var(--danger); }
.mini {
  font-size: 12px;
  color: var(--ink-3);
  margin-top: 4px;
}
.grid-2 {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
}
.stat-tile {
  background: var(--bg);
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 12px;
}
.stat-tile .num {
  font-size: 22px;
  font-weight: 700;
  letter-spacing: -0.02em;
}
.stat-tile .lab {
  font-size: 12px;
  color: var(--ink-2);
  margin-top: 2px;
}
.status-line:last-child { border-bottom: 0; }
.pill {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 3px 10px;
  border-radius: 999px;
  font-size: 12px;
  background: var(--accent-soft);
  color: var(--accent);
  font-weight: 600;
}
.pill.warn {
  background: #f8e8d8;
  color: #9a5b1b;
}
.pill.bad {
  background: #f6e0de;
  color: var(--danger);
}

/* ===== Memory ===== */
.memory-editor {
  width: 100%;
  min-height: 55vh;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 14px;
  background: var(--card);
  box-shadow: var(--shadow);
  resize: vertical;
  outline: none;
  line-height: 1.7;
  font-size: 14px;
}
.memory-editor:focus { border-color: var(--accent); }

/* ===== Tabbar ===== */
.tabbar {
  position: fixed;
  left: 50%;
  transform: translateX(-50%);
  bottom: 0;
  width: min(820px, 100%);
  height: var(--tabbar-h);
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  background: rgba(255, 255, 255, 0.92);
  border-top: 1px solid var(--line);
  backdrop-filter: blur(12px);
  z-index: 30;
  padding-bottom: env(safe-area-inset-bottom);
}
.tab {
  border: 0;
  background: transparent;
  color: var(--ink-3);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
  font-size: 12px;
  cursor: pointer;
}
.tab .tab-ico { font-size: 18px; line-height: 1; }
.tab.active {
  color: var(--accent);
  font-weight: 700;
}

@media (min-width: 860px) {
  .messages { min-height: 52vh; }
  .msg { max-width: 72%; }
}
