:root {
  --bg: #f6f7fb;
  --card: #ffffff;
  --text: #171b26;
  --muted: #6b7180;
  --line: #e4e7ef;
  --field: #f9fafc;
  --accent: #2a6fdb;
  --accent-soft: #e8f0fd;
  --accent-text: #ffffff;
  --yellow: #f5c542;
  --danger: #c93a3a;
  --danger-soft: #fdeeee;
  --radius: 16px;
  --shadow: 0 1px 2px rgba(20, 30, 60, 0.04), 0 8px 24px rgba(20, 30, 60, 0.06);
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f1218;
    --card: #171b23;
    --text: #eef0f5;
    --muted: #949bab;
    --line: #2a303b;
    --field: #1d222c;
    --accent: #5b95f0;
    --accent-soft: #1d2c46;
    --accent-text: #0b1220;
    --danger: #f07a7a;
    --danger-soft: #3a1f22;
    --shadow: none;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }

html, body { margin: 0; }

body {
  background: var(--bg);
  color: var(--text);
  font: 16px/1.5 -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", Roboto, "Helvetica Neue", "PingFang TC", "Microsoft JhengHei", sans-serif;
  -webkit-font-smoothing: antialiased;
  -webkit-text-size-adjust: 100%;
}

[hidden] { display: none !important; }

/* ---------- Top bar ---------- */
.topbar {
  position: sticky; top: 0; z-index: 10;
  display: flex; align-items: center; justify-content: space-between;
  padding: calc(10px + env(safe-area-inset-top)) 20px 10px;
  background: color-mix(in srgb, var(--bg) 85%, transparent);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
}
.brand { display: flex; align-items: center; gap: 10px; font-weight: 650; letter-spacing: -0.01em; }
.brand img { width: 30px; height: 30px; border-radius: 8px; }

/* ---------- Tabs ---------- */
.tabs { display: flex; gap: 4px; background: var(--field); border: 1px solid var(--line); border-radius: 12px; padding: 3px; }
.tabs button {
  appearance: none; border: 0; cursor: pointer; background: transparent;
  font: inherit; font-size: 14px; font-weight: 600; color: var(--muted);
  padding: 6px 14px; border-radius: 9px; min-height: 34px;
}
.tabs button[aria-current="page"] { background: var(--card); color: var(--text); box-shadow: 0 1px 3px rgba(0,0,0,.08); }

/* ---------- Layout ---------- */
.layout {
  display: grid; gap: 16px;
  max-width: 1160px; margin: 0 auto;
  padding: 4px 16px calc(32px + env(safe-area-inset-bottom));
}
@media (min-width: 960px) {
  .layout { grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); align-items: start; padding: 8px 24px 40px; }
  .result { position: sticky; top: 58px; }
}

.card {
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 20px;
  display: flex; flex-direction: column; gap: 18px;
}

/* ---------- Fields ---------- */
label { font-size: 13px; font-weight: 600; color: var(--muted); letter-spacing: 0.01em; }
.optional { font-weight: 400; opacity: 0.8; }
.row-head { display: flex; align-items: baseline; justify-content: space-between; gap: 8px; margin-bottom: 8px; }
.field > label:first-child { display: block; margin-bottom: 8px; }
.hint { font-size: 12px; color: var(--muted); margin: 6px 2px 0; }

textarea, input[type="text"], input[type="password"] {
  width: 100%;
  font: inherit; color: inherit;
  background: var(--field);
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 12px 14px;
  outline: none;
  transition: border-color .15s, box-shadow .15s;
}
textarea { resize: vertical; min-height: 48px; line-height: 1.55; }
textarea:focus, input:focus { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
::placeholder { color: var(--muted); opacity: 0.75; }

/* ---------- Buttons ---------- */
.btn {
  appearance: none; cursor: pointer;
  font: inherit; font-weight: 600; font-size: 15px;
  color: var(--text);
  background: var(--field);
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 11px 16px;
  min-height: 44px;
  transition: transform .08s, background .15s, opacity .15s;
}
.btn:active { transform: scale(0.98); }
.btn:disabled { opacity: 0.5; cursor: default; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }
.btn.ghost { background: transparent; border-color: transparent; color: var(--muted); }
.btn.small { min-height: 36px; padding: 6px 12px; font-size: 14px; }
.btn.block { width: 100%; }
.grow { flex: 1; }

.link {
  appearance: none; border: 0; background: none; cursor: pointer;
  font: inherit; font-size: 13px; font-weight: 600; color: var(--accent);
  padding: 4px 2px;
}

.actions { display: flex; gap: 10px; }
.result-actions { display: flex; flex-wrap: wrap; gap: 8px; }
.result-actions .btn { flex: 1 1 auto; }
.result-actions #copy { flex-basis: 100%; }
@media (min-width: 520px) { .result-actions #copy { flex-basis: auto; } }

/* ---------- Segmented control ---------- */
.segmented {
  display: grid; grid-auto-flow: column; grid-auto-columns: 1fr;
  background: var(--field); border: 1px solid var(--line);
  border-radius: 12px; padding: 4px; gap: 4px;
}
.segmented button {
  appearance: none; border: 0; cursor: pointer;
  font: inherit; font-weight: 600; font-size: 15px; color: var(--muted);
  background: transparent; border-radius: 9px; padding: 8px 12px; min-height: 38px;
}
.segmented button[aria-selected="true"] { background: var(--card); color: var(--text); box-shadow: 0 1px 3px rgba(0,0,0,.08); }
.segmented.small { max-width: 260px; }

/* ---------- Chips ---------- */
.chips { display: flex; flex-wrap: wrap; gap: 8px; }
.chip {
  appearance: none; cursor: pointer;
  display: inline-flex; align-items: center; gap: 6px;
  font: inherit; font-size: 14px; font-weight: 500; color: var(--text);
  background: var(--field); border: 1px solid var(--line);
  border-radius: 999px; padding: 7px 14px; min-height: 36px;
  transition: background .15s, border-color .15s, color .15s;
}
.chip[aria-pressed="true"] { background: var(--accent-soft); border-color: var(--accent); color: var(--accent); font-weight: 600; }
.chip .x { font-size: 16px; line-height: 1; color: var(--danger); margin-left: 2px; }
.editing .chip { cursor: default; }
.chip-add { display: inline-flex; gap: 6px; }
.chip-add input { width: 150px; padding: 7px 12px; border-radius: 999px; font-size: 14px; }
.chip-add .btn { min-height: 36px; padding: 6px 12px; border-radius: 999px; font-size: 14px; }
.chip-group.editing .edit-toggle { color: var(--text); }

/* ---------- Result ---------- */
.result { min-height: 220px; position: relative; scroll-margin-top: 64px; }
.empty { margin: auto; text-align: center; color: var(--muted); padding: 32px 0; }
.empty img { width: 56px; height: 56px; opacity: 0.35; filter: grayscale(0.4); border-radius: 14px; }
#result-body { display: flex; flex-direction: column; gap: 18px; }
#output { min-height: 220px; }
.chinese {
  white-space: pre-wrap;
  background: var(--field); border: 1px dashed var(--line);
  border-radius: 12px; padding: 12px 14px; color: var(--muted); font-size: 15px;
}

.busy {
  position: absolute; inset: 0; border-radius: var(--radius);
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 12px;
  background: color-mix(in srgb, var(--card) 88%, transparent);
  color: var(--muted);
}
.spinner {
  width: 28px; height: 28px; border-radius: 50%;
  border: 3px solid var(--accent-soft); border-top-color: var(--accent);
  animation: spin .8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* ---------- Pages (History, Style, Settings) ---------- */
.page { display: grid; gap: 16px; max-width: 720px; margin: 0 auto; padding: 4px 16px calc(32px + env(safe-area-inset-bottom)); align-content: start; }
.page h2 { margin: 0 0 4px; font-size: 17px; letter-spacing: -0.01em; }
.small-text { font-size: 14px; margin: 0; }
.toggle { display: flex; gap: 10px; align-items: flex-start; font-size: 15px; font-weight: 500; color: var(--text); cursor: pointer; }
.toggle input { width: 20px; height: 20px; margin: 1px 0 0; accent-color: var(--accent); flex: none; }
.project-note { display: block; margin-bottom: 14px; }
.project-note:last-child { margin-bottom: 0; }
.project-note span { display: block; margin-bottom: 6px; }
.project-note textarea { font-size: 16px; font-weight: 400; color: var(--text); letter-spacing: normal; }
.empty-text { color: var(--muted); font-size: 14px; margin: 0; }
.center { text-align: center; padding: 40px 0; }
.field-label { display: flex; flex-direction: column; gap: 6px; }
.habit-list { display: grid; gap: 8px; }
.habit { display: flex; gap: 6px; align-items: flex-start; }
.habit textarea { flex: 1; min-height: 44px; padding: 10px 12px; }
.habit .remove { flex: none; width: 36px; height: 44px; font-size: 20px; line-height: 1; }
.field-label input, .field-label textarea, #tone-rules textarea, #examples textarea, .habit textarea {
  font-size: 16px; font-weight: 400; color: var(--text); letter-spacing: normal;
}
.example { display: flex; flex-direction: column; gap: 6px; margin-bottom: 14px; }
.example-head { display: flex; justify-content: space-between; align-items: center; }
.danger-text, .btn.danger-text, .link.danger-text { color: var(--danger); }

/* ---------- History ---------- */
.page-head { display: flex; gap: 8px; align-items: center; }
.page-head input { flex: 1; }
input[type="search"] {
  width: 100%; font: inherit; color: inherit; background: var(--card);
  border: 1px solid var(--line); border-radius: 12px; padding: 11px 14px; outline: none;
}
input[type="search"]:focus { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
.history-list { display: grid; gap: 10px; }
.h-item { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow); }
.h-item summary { list-style: none; cursor: pointer; padding: 14px 16px; display: grid; gap: 4px; }
.h-item summary::-webkit-details-marker { display: none; }
.h-meta { font-size: 12px; color: var(--muted); display: flex; flex-wrap: wrap; gap: 6px; }
.h-tag { background: var(--field); border: 1px solid var(--line); border-radius: 999px; padding: 1px 8px; }
.h-title { font-weight: 600; }
.h-preview { color: var(--muted); font-size: 14px; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; }
.h-item[open] .h-preview { display: none; }
.h-body { padding: 0 16px 16px; display: grid; gap: 12px; }
.h-text { white-space: pre-wrap; margin: 0; font-size: 15px; }
.h-actions { display: flex; flex-wrap: wrap; gap: 8px; }

/* ---------- Learn from edits ---------- */
.learn { background: var(--accent-soft); border-radius: 12px; padding: 12px 14px; display: grid; gap: 10px; font-size: 14px; }
.learn p { margin: 0; }
.learn-actions { display: flex; gap: 8px; justify-content: flex-end; }

@media (max-width: 520px) {
  .brand-name { display: none; }
  .tabs button { padding: 6px 10px; }
}

/* ---------- What was sent ---------- */
.sent { font-size: 14px; color: var(--muted); }
.sent summary { cursor: pointer; font-weight: 600; color: var(--accent); }
.sent pre {
  white-space: pre-wrap; word-break: break-word; margin: 10px 0 0;
  font: 13px/1.5 ui-monospace, SFMono-Regular, Menlo, monospace;
  background: var(--field); border: 1px solid var(--line); border-radius: 12px; padding: 12px;
  max-height: 260px; overflow: auto;
}

/* ---------- Messages ---------- */
.error {
  margin: 0; padding: 10px 14px; border-radius: 12px;
  background: var(--danger-soft); color: var(--danger); font-size: 14px;
}
.muted { color: var(--muted); }

.toast {
  position: fixed; left: 50%; bottom: calc(24px + env(safe-area-inset-bottom));
  transform: translateX(-50%);
  background: var(--text); color: var(--bg);
  padding: 10px 18px; border-radius: 999px; font-size: 14px; font-weight: 600;
  box-shadow: 0 8px 24px rgba(0,0,0,.18); z-index: 50;
}

/* ---------- Login ---------- */
.login { min-height: 100dvh; display: grid; place-items: center; padding: 16px; }
.login-card {
  width: 100%; max-width: 360px; text-align: center;
  display: flex; flex-direction: column; gap: 14px;
  background: var(--card); border: 1px solid var(--line); border-radius: 20px;
  box-shadow: var(--shadow); padding: 32px 24px;
}
.login-card h1 { margin: 0; font-size: 22px; letter-spacing: -0.01em; }
.login-card p { margin: 0; }
.login-icon { width: 72px; height: 72px; margin: 0 auto; border-radius: 18px; }
