/* Berggasse — design tokens ------------------------------------------------ */
:root {
  --bg: #f3eee4;          --surface: #fffdf8;      --surface-2: #f8f4ec;
  --ink: #221c18;         --ink-2: #4d453e;        --muted: #857a70;
  --line: #e4dccd;        --line-2: #d2c8b6;
  --accent: #6b3d8f;      --accent-ink: #fff;      --accent-soft: rgba(107, 61, 143, .10);
  --id: #b4432b;    --id-soft: rgba(180, 67, 43, .10);
  --superego: #2f4f8e; --superego-soft: rgba(47, 79, 142, .10);
  --ego: #3b7a4f;   --ego-soft: rgba(59, 122, 79, .10);
  --warn-bg: #f7ead0; --warn-ink: #6b4a06; --ok-bg: #dcefdf; --ok-ink: #1f5a30; --err-bg: #f7dcd7; --err-ink: #8a2a1b;
  --radius: 10px; --radius-sm: 6px;
  --shadow: 0 1px 2px rgba(40, 30, 20, .06), 0 6px 20px -12px rgba(40, 30, 20, .25);
  --font: -apple-system, "PingFang SC", "Hiragino Sans GB", "Noto Sans CJK SC", "Microsoft YaHei", "Helvetica Neue", Arial, sans-serif;
  --serif: "Fraunces", "Songti SC", "Noto Serif CJK SC", Georgia, serif;
  --mono: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  --sidebar: 228px;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #161310; --surface: #1f1b17; --surface-2: #26211c;
    --ink: #ece5d8; --ink-2: #c8bfb2; --muted: #958a7d;
    --line: #322c25; --line-2: #453d33;
    --accent: #b08ad1; --accent-ink: #1a1020; --accent-soft: rgba(176, 138, 209, .14);
    --id: #e0705a; --id-soft: rgba(224, 112, 90, .14);
    --superego: #7f9ee0; --superego-soft: rgba(127, 158, 224, .14);
    --ego: #6fb585; --ego-soft: rgba(111, 181, 133, .14);
    --warn-bg: #3e3113; --warn-ink: #f0d79a; --ok-bg: #1c3523; --ok-ink: #a8dcb5; --err-bg: #43201b; --err-ink: #f2b0a4;
    --shadow: 0 1px 2px rgba(0,0,0,.4), 0 8px 24px -12px rgba(0,0,0,.6);
  }
}

/* base --------------------------------------------------------------------- */
* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body { margin: 0; background: var(--bg); color: var(--ink); font: 14.5px/1.6 var(--font); font-feature-settings: "tnum"; }
a { color: var(--accent); text-decoration: none; } a:hover { text-decoration: underline; }
code { font: 12.5px/1.4 var(--mono); background: var(--surface-2); border: 1px solid var(--line); padding: 1px 5px; border-radius: 4px; }
pre { white-space: pre-wrap; word-break: break-word; font: 12.5px/1.5 var(--mono); }
h1, h2, h3 { margin: 0; line-height: 1.25; }
h1 { font: 500 26px/1.2 var(--serif); letter-spacing: -.01em; }
h2 { font: 600 14px/1.3 var(--font); }
p { margin: 0 0 8px; }
.muted { color: var(--muted); } .small { font-size: 12.5px; } .err { color: var(--err-ink); }
.serif { font-family: var(--serif); }

/* app shell ---------------------------------------------------------------- */
.shell { display: grid; grid-template-columns: var(--sidebar) 1fr; min-height: 100vh; }
.side { background: var(--surface); border-right: 1px solid var(--line); padding: 18px 14px; position: sticky; top: 0; height: 100vh; display: flex; flex-direction: column; gap: 6px; overflow-y: auto; }
.brand { display: flex; align-items: baseline; gap: 8px; padding: 4px 8px 14px; color: var(--ink); }
.brand .name { font: 600 20px/1 var(--serif); letter-spacing: .01em; }
.brand .tag { font-size: 11px; color: var(--muted); letter-spacing: .08em; text-transform: uppercase; }
.side .group { font-size: 11px; letter-spacing: .08em; text-transform: uppercase; color: var(--muted); padding: 12px 8px 4px; }
.side a.item { display: flex; align-items: center; gap: 9px; padding: 7px 9px; border-radius: var(--radius-sm); color: var(--ink-2); font-weight: 500; }
.side a.item:hover { background: var(--surface-2); text-decoration: none; color: var(--ink); }
.side a.item.active { background: var(--accent-soft); color: var(--accent); }
.side a.item .dot { width: 8px; height: 8px; border-radius: 50%; background: var(--line-2); flex: none; }
.side a.item.role-id .dot { background: var(--id); } .side a.item.role-superego .dot { background: var(--superego); } .side a.item.role-ego .dot { background: var(--ego); }
.side .spacer { flex: 1; }
.side .me { border-top: 1px solid var(--line); padding: 12px 8px 0; display: flex; justify-content: space-between; align-items: center; gap: 8px; }
.side .me .who { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.main { min-width: 0; }
.page { padding: 28px 36px 60px; max-width: 1360px; }
.page-head { display: flex; align-items: flex-end; justify-content: space-between; gap: 16px; margin-bottom: 20px; flex-wrap: wrap; }
.page-head .sub { color: var(--muted); margin-top: 4px; }
.page-head .actions { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.banner { padding: 9px 36px; font-size: 13.5px; }
.banner.warn { background: var(--warn-bg); color: var(--warn-ink); } .banner.ok { background: var(--ok-bg); color: var(--ok-ink); }
.banner a { color: inherit; text-decoration: underline; }

/* cards, chips, buttons ---------------------------------------------------- */
.card { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 18px 20px; margin-bottom: 16px; box-shadow: var(--shadow); }
.card-head { display: flex; align-items: baseline; justify-content: space-between; gap: 12px; margin-bottom: 12px; }
.card-head .hint { color: var(--muted); font-size: 12.5px; }
.chip { display: inline-flex; align-items: center; gap: 5px; font-size: 12px; line-height: 1; padding: 5px 9px; border-radius: 999px; border: 1px solid var(--line-2); background: var(--surface-2); color: var(--ink-2); white-space: nowrap; }
.chip.bad { border-color: var(--id); color: var(--id); background: var(--id-soft); }
.chip.good { border-color: var(--ego); color: var(--ego); background: var(--ego-soft); }
.chip.accent { border-color: transparent; background: var(--accent-soft); color: var(--accent); }
.chips { display: flex; gap: 6px; flex-wrap: wrap; }
.btn { display: inline-flex; align-items: center; gap: 6px; border: 1px solid var(--line-2); background: var(--surface); color: var(--ink); padding: 7px 13px; border-radius: var(--radius-sm); cursor: pointer; font: 500 13.5px var(--font); line-height: 1.2; transition: background .12s, border-color .12s; }
.btn:hover { background: var(--surface-2); text-decoration: none; }
.btn.primary { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); } .btn.primary:hover { filter: brightness(1.08); }
.btn.small { padding: 4px 9px; font-size: 12.5px; } .btn.wide { width: 100%; justify-content: center; } .btn.danger { color: var(--err-ink); }
.btn.ghost { border-color: transparent; background: transparent; } .btn.ghost:hover { background: var(--surface-2); }
.btn:disabled { opacity: .5; cursor: default; }
.label { font-size: 11.5px; font-weight: 600; letter-spacing: .06em; text-transform: uppercase; color: var(--muted); margin-bottom: 6px; }
.empty { color: var(--muted); padding: 48px 24px; text-align: center; border: 1px dashed var(--line-2); border-radius: var(--radius); }

/* forms -------------------------------------------------------------------- */
label { display: block; font-size: 13px; color: var(--ink-2); font-weight: 500; }
label > input, label > select, label > textarea { margin-top: 5px; }
input:not([type=checkbox]):not([type=file]), select, textarea { width: 100%; font: inherit; color: var(--ink); background: var(--surface); border: 1px solid var(--line-2); border-radius: var(--radius-sm); padding: 8px 10px; transition: border-color .12s, box-shadow .12s; }
input:focus, select:focus, textarea:focus { outline: none; border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
textarea { resize: vertical; line-height: 1.55; } textarea.mono { font: 13px/1.55 var(--mono); }
input[type=number] { max-width: 140px; }
input[type=checkbox] { width: 16px; height: 16px; accent-color: var(--accent); margin: 0; }
label.check { display: flex; align-items: center; gap: 9px; font-weight: 500; padding: 4px 0; }
label.check .muted { font-weight: 400; }
.grid { display: grid; gap: 14px 18px; } .grid.c2 { grid-template-columns: repeat(2, minmax(0, 1fr)); } .grid.c3 { grid-template-columns: repeat(3, minmax(0, 1fr)); } .grid.c4 { grid-template-columns: repeat(4, minmax(0, 1fr)); }
.row { display: flex; gap: 12px; flex-wrap: wrap; align-items: flex-end; } .row > * { flex: 1; min-width: 160px; } .row > button, .row > form, .row > .btn { flex: 0 0 auto; }
.inline-form { display: inline-flex; gap: 6px; align-items: center; } .inline-form input { width: auto; }
.help { color: var(--muted); font-size: 12.5px; margin-top: 6px; }
.savebar { position: sticky; bottom: 0; display: flex; gap: 10px; align-items: center; padding: 12px 0; margin-top: 4px; background: linear-gradient(to top, var(--bg) 70%, transparent); }
.savebar .grow { flex: 1; }

/* tables ------------------------------------------------------------------- */
.table { width: 100%; border-collapse: collapse; background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); overflow: hidden; box-shadow: var(--shadow); }
.table th, .table td { padding: 9px 12px; text-align: left; vertical-align: middle; border-bottom: 1px solid var(--line); }
.table th { font-size: 11.5px; font-weight: 600; letter-spacing: .06em; text-transform: uppercase; color: var(--muted); background: var(--surface-2); }
.table tr:last-child td { border-bottom: 0; } .table tbody tr:hover td { background: var(--surface-2); }
.table tr.active td { background: var(--accent-soft); }
.table-wrap { overflow-x: auto; }

/* voices (id / superego / ego) -------------------------------------------- */
.voice { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 12px 14px; white-space: pre-wrap; position: relative; box-shadow: var(--shadow); }
.voice .vh { display: flex; align-items: center; gap: 8px; margin-bottom: 8px; white-space: normal; }
.voice .vh .name { font: 600 12px/1 var(--font); letter-spacing: .05em; padding: 5px 9px; border-radius: 999px; }
.voice .vh .sub { font-size: 11.5px; color: var(--muted); font-family: var(--mono); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.voice.id { border-top: 3px solid var(--id); } .voice.id .name { background: var(--id-soft); color: var(--id); }
.voice.superego { border-top: 3px solid var(--superego); } .voice.superego .name { background: var(--superego-soft); color: var(--superego); }
.voice.ego { border-top: 3px solid var(--ego); } .voice.ego .name { background: var(--ego-soft); color: var(--ego); }
.voice.user { border-left: 3px solid var(--line-2); background: var(--surface-2); box-shadow: none; }
.voice.user .name { background: transparent; color: var(--muted); padding-left: 0; }
.voice.errbox { background: var(--err-bg); color: var(--err-ink); border-color: transparent; }
.voice .tension { margin-top: 12px; padding: 10px 12px; border-radius: var(--radius-sm); background: var(--surface-2); color: var(--ink-2); font-size: 13px; border-left: 3px solid var(--line-2); }
.voice .tension::before { content: "张力说明"; display: block; font-size: 11px; letter-spacing: .06em; text-transform: uppercase; color: var(--muted); margin-bottom: 4px; }
.raw-grid { display: grid; grid-template-columns: 1fr 1fr 1.35fr; gap: 12px; }
.turn { margin-bottom: 26px; } .turn > * + * { margin-top: 10px; }
.turn .foot { color: var(--muted); font-size: 12px; display: flex; gap: 10px; flex-wrap: wrap; }
.pending .running { display: flex; align-items: center; gap: 12px; color: var(--muted); font-size: 13px; padding: 4px 2px; }
.pending .running .stage { display: inline-flex; gap: 6px; align-items: center; }
.pending .running .stage i { width: 8px; height: 8px; border-radius: 50%; animation: pulse 1.4s infinite ease-in-out; }
.pending .running .stage.id i { background: var(--id); } .pending .running .stage.superego i { background: var(--superego); animation-delay: .2s; } .pending .running .stage.ego i { background: var(--ego); animation-delay: .5s; }
@keyframes pulse { 0%, 100% { opacity: .25; transform: scale(.8); } 50% { opacity: 1; transform: scale(1.05); } }

/* chat --------------------------------------------------------------------- */
.chat { display: grid; grid-template-columns: 260px 1fr; height: 100vh; }
.sessions { border-right: 1px solid var(--line); background: var(--surface); padding: 16px 12px; overflow-y: auto; display: flex; flex-direction: column; gap: 4px; }
.sessions .list { list-style: none; padding: 0; margin: 10px 0 0; display: flex; flex-direction: column; gap: 2px; }
.sessions li a { display: block; padding: 8px 10px; border-radius: var(--radius-sm); color: var(--ink); }
.sessions li a:hover { background: var(--surface-2); text-decoration: none; } .sessions li.active a { background: var(--accent-soft); }
.sessions li .t { display: block; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-weight: 500; }
.sessions li .m { display: block; font-size: 11.5px; color: var(--muted); margin-top: 2px; }
.conv { display: flex; flex-direction: column; min-width: 0; height: 100vh; }
.conv-head { display: flex; align-items: center; gap: 12px; padding: 14px 28px; border-bottom: 1px solid var(--line); background: var(--surface); flex-wrap: wrap; }
.conv-head .title { font: 500 17px/1.2 var(--serif); }
.conv-head .meta { color: var(--muted); font-size: 12.5px; display: flex; gap: 10px; flex-wrap: wrap; }
.turns { flex: 1; overflow-y: auto; padding: 24px 28px; }
.composer { padding: 14px 28px 18px; border-top: 1px solid var(--line); background: var(--surface); }
.composer .box { display: flex; gap: 10px; align-items: flex-end; background: var(--surface); border: 1px solid var(--line-2); border-radius: 12px; padding: 8px 8px 8px 14px; }
.composer .box:focus-within { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
.composer textarea { border: 0; padding: 6px 0; background: transparent; box-shadow: none !important; min-height: 44px; max-height: 240px; }
.composer .hint { color: var(--muted); font-size: 11.5px; margin-top: 6px; }
.conv .empty-state { margin: auto; max-width: 460px; text-align: center; color: var(--muted); padding: 40px; }
.conv .empty-state .big { font: 500 22px/1.3 var(--serif); color: var(--ink); margin-bottom: 8px; }

/* rules editor ------------------------------------------------------------- */
.rules-layout { display: grid; grid-template-columns: minmax(0, 1fr) 340px; gap: 20px; align-items: start; }
.rule { padding: 14px 16px; }
.rule .head { display: flex; gap: 10px; align-items: center; margin-bottom: 10px; }
.rule .head .f-id { width: 96px; font-family: var(--mono); font-weight: 600; }
.rule .head select { width: auto; }
.rule .head .grow { flex: 1; }
.rule.prohibition { border-left: 3px solid var(--superego); } .rule.ideal { border-left: 3px solid var(--ego); }
.rule textarea { min-height: 40px; }
.try { position: sticky; top: 20px; }
.try pre { background: var(--surface-2); padding: 10px 12px; border-radius: var(--radius-sm); min-height: 90px; margin: 10px 0 0; }
pre.diff { background: var(--surface); border: 1px solid var(--line); padding: 14px 16px; border-radius: var(--radius); }
pre.diff .add { color: var(--ego); background: var(--ego-soft); display: inline-block; width: 100%; } pre.diff .del { color: var(--id); background: var(--id-soft); display: inline-block; width: 100%; } pre.diff .hunk { color: var(--superego); }

/* metrics ------------------------------------------------------------------ */
.stats { display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 10px; margin: 12px 0; }
.stat { background: var(--surface-2); border: 1px solid var(--line); border-radius: var(--radius-sm); padding: 10px 12px; }
.stat .v { font: 500 22px/1.1 var(--serif); margin-bottom: 3px; } .stat .v.none { color: var(--muted); }
.stat .k { font-size: 11.5px; color: var(--muted); }
.stat.id .v { color: var(--id); } .stat.superego .v { color: var(--superego); } .stat.ego .v { color: var(--ego); }
.bars { display: flex; flex-direction: column; gap: 4px; }
.bars .bar { display: grid; grid-template-columns: 56px 1fr 32px; align-items: center; gap: 8px; font-size: 12.5px; }
.bars .bar span { color: var(--muted); font-family: var(--mono); } .bars .bar i { display: block; height: 10px; background: var(--accent); border-radius: 3px; min-width: 2px; opacity: .85; }
.bars .bar b { font-weight: 500; text-align: right; }
.annotation { margin-top: 12px; padding-top: 12px; border-top: 1px dashed var(--line-2); display: flex; gap: 10px; align-items: center; flex-wrap: wrap; }
details summary { cursor: pointer; color: var(--muted); font-size: 12.5px; }

/* login -------------------------------------------------------------------- */
.login { min-height: 100vh; display: grid; place-items: center; padding: 24px; }
.login .card { max-width: 440px; width: 100%; padding: 32px; }
.login .name { font: 500 34px/1.1 var(--serif); margin-bottom: 4px; }
.login .addr { color: var(--muted); font-size: 12.5px; letter-spacing: .08em; text-transform: uppercase; margin-bottom: 20px; }

/* responsive --------------------------------------------------------------- */
@media (max-width: 980px) {
  .shell { grid-template-columns: 1fr; }
  .side { position: static; height: auto; flex-direction: row; flex-wrap: wrap; align-items: center; gap: 2px; padding: 10px 12px; border-right: 0; border-bottom: 1px solid var(--line); }
  .side .group { display: none; } .side .spacer { display: none; } .side .brand { padding: 4px 8px; } .side .me { border: 0; padding: 0 8px; }
  .page { padding: 18px; }
  .chat { grid-template-columns: 1fr; height: auto; } .sessions { max-height: 200px; border-right: 0; border-bottom: 1px solid var(--line); }
  .conv { height: auto; min-height: 70vh; } .turns { padding: 16px; } .conv-head, .composer { padding-left: 16px; padding-right: 16px; }
  .raw-grid, .rules-layout, .grid.c2, .grid.c3, .grid.c4 { grid-template-columns: 1fr; }
  .try { position: static; }
}

/* mobile ------------------------------------------------------------------- */
.brand { justify-content: space-between; width: 100%; }
.brand-link { color: inherit; display: inline-flex; align-items: baseline; gap: 6px; } .brand-link:hover { text-decoration: none; }
.menu-btn, .sessions-toggle { display: none; }
@media (max-width: 980px) {
  .side { padding: 0; gap: 0; }
  .side .brand { padding: 10px 14px; margin: 0; }
  .menu-btn { display: inline-flex; font-size: 18px; padding: 4px 10px; }
  .side .nav { display: none; padding: 4px 8px 10px; grid-template-columns: repeat(2, 1fr); gap: 2px; width: 100%; border-top: 1px solid var(--line); }
  .side .group { display: block; grid-column: 1 / -1; padding: 10px 8px 2px; }
  .side.open .nav { display: grid; }
  .side .me { display: none; }
  .side.open .me { display: flex; width: 100%; padding: 10px 16px 12px; border-top: 1px solid var(--line); }
  .banner { padding: 9px 16px; }
  .page-head { margin-bottom: 14px; } h1 { font-size: 22px; }
  .card { padding: 14px; border-radius: var(--radius-sm); }
  .stats { grid-template-columns: repeat(2, 1fr); }
  input:not([type=checkbox]):not([type=file]), select, textarea { font-size: 16px; }  /* stop iOS zoom */
  input[type=number] { max-width: none; }
  .savebar { padding-bottom: calc(12px + env(safe-area-inset-bottom)); }
  /* chat: sessions become a drawer */
  .chat { display: block; height: auto; }
  .sessions { display: none; position: fixed; inset: 0; z-index: 30; max-height: none; padding: 16px; padding-top: calc(16px + env(safe-area-inset-top)); border: 0; }
  .sessions.open { display: flex; }
  .sessions .list { flex: 1; overflow-y: auto; }
  .sessions-toggle { display: inline-flex; }
  .conv { height: calc(100vh - 53px); height: calc(100dvh - 53px); }
  .conv-head { padding: 10px 14px; gap: 8px; } .conv-head .title { font-size: 15px; flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
  .conv-head .meta { width: 100%; font-size: 12px; }
  .turns { padding: 14px; } .turn { margin-bottom: 20px; }
  .composer { padding: 10px 12px calc(12px + env(safe-area-inset-bottom)); }
  .composer .hint { display: none; }
  .voice { padding: 10px 12px; }
}
@media (max-width: 980px) { .sessions .drawer-close { display: inline-flex; } }
.sessions .drawer-close { display: none; margin-bottom: 8px; }
