/* ---------------------------------------------------------------------------
   Tokens. Quiet Apple-like palette: one accent, few borders, generous radii.
   Every label sits on a single line; only the prompt editors wrap.
--------------------------------------------------------------------------- */
:root {
  color-scheme: light;
  --font-sans: -apple-system, BlinkMacSystemFont, "SF Pro Text", "Helvetica Neue", Inter, "PingFang TC", "Noto Sans TC", "Microsoft JhengHei", sans-serif;
  --font-mono: "SF Mono", ui-monospace, Menlo, Consolas, "Noto Sans Mono CJK TC", monospace;

  --text-xs: 12px;
  --text-sm: 13px;
  --text-base: 14px;
  --text-md: 15px;
  --text-lg: 17px;
  --text-xl: 21px;

  --space-1: 4px;
  --space-2: 8px;
  --space-3: 12px;
  --space-4: 16px;
  --space-5: 20px;
  --space-6: 24px;

  --radius-sm: 8px;
  --radius-md: 12px;
  --radius-lg: 16px;
  --radius-pill: 999px;

  --dur-fast: 120ms;
  --dur: 180ms;
  --ease: cubic-bezier(.2, .7, .2, 1);

  --topbar-h: 60px;
  --control-h: 40px;

  --bg: #f5f5f7;
  --panel: #ffffff;
  --text: #1d1d1f;
  --muted: #6e6e73;
  --border: rgba(0, 0, 0, .08);
  --border-strong: rgba(0, 0, 0, .18);
  --accent: #0071e3;
  --accent-hover: #0077ed;
  --on-accent: #ffffff;
  --soft: rgba(0, 113, 227, .10);
  --input: #f5f5f7;
  --success: #1d8a45;
  --success-soft: rgba(52, 199, 89, .14);
  --danger: #d70015;
  --danger-soft: rgba(255, 59, 48, .12);
  --warn: #b25000;
  --shadow-sm: 0 1px 2px rgba(0, 0, 0, .06);
  --shadow-lg: 0 16px 48px rgba(0, 0, 0, .14);
  --tip-bg: rgba(29, 29, 31, .94);
  --tip-fg: #ffffff;
}

body.dark {
  color-scheme: dark;
  --bg: #000000;
  --panel: #1c1c1e;
  --text: #f5f5f7;
  --muted: #98989d;
  --border: rgba(255, 255, 255, .10);
  --border-strong: rgba(255, 255, 255, .22);
  --accent: #0a84ff;
  --accent-hover: #409cff;
  --soft: rgba(10, 132, 255, .18);
  --input: #2c2c2e;
  --success: #30d158;
  --success-soft: rgba(48, 209, 88, .16);
  --danger: #ff453a;
  --danger-soft: rgba(255, 69, 58, .16);
  --warn: #ff9f0a;
  --shadow-sm: none;
  --shadow-lg: 0 20px 60px rgba(0, 0, 0, .6);
  --tip-bg: rgba(245, 245, 247, .96);
  --tip-fg: #1d1d1f;
}

/* ---------------------------------------------------------------------------
   Base
--------------------------------------------------------------------------- */
* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  width: 100%;
  height: 100dvh;
  overflow: hidden;
  background: var(--bg);
  color: var(--text);
  font: var(--text-base)/1.4 var(--font-sans);
  -webkit-font-smoothing: antialiased;
  white-space: nowrap;
}
/* Only free-text editors and raw output may break lines. */
textarea { white-space: pre-wrap; }
.raw-text { white-space: pre; }
h1, h2, h3, p { margin: 0; }
button, input, select, textarea { font: inherit; color: inherit; }
button, select, [role="option"] { cursor: pointer; touch-action: manipulation; }
.manual-link { display: inline-flex; align-items: center; min-height: 32px; padding: 0 var(--space-2); border-radius: var(--radius-sm); color: var(--muted); font-size: var(--text-sm); font-weight: 600; text-decoration: none; white-space: nowrap; }
.manual-link:hover, .manual-link:focus-visible { color: var(--accent); background: var(--soft); outline: 0; }
[hidden] { display: none !important; }
.grow { flex: 1; min-width: 0; }
.truncate, .chip, .state, #prompt-select-label, .result-key, .result-value, .notice span, .meta-chip, .connection { overflow: hidden; text-overflow: ellipsis; }

.icon { width: 18px; height: 18px; flex: none; stroke: currentColor; fill: none; stroke-width: 1.75; stroke-linecap: round; stroke-linejoin: round; }
.icon-sm { width: 15px; height: 15px; }
.icon-xl { width: 36px; height: 36px; stroke-width: 1.25; }

:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; border-radius: var(--radius-sm); }
input:focus-visible, select:focus-visible, textarea:focus-visible { outline-offset: 0; border-color: var(--accent); }

/* Tooltips: hover or focus shows the explanation below the control. --------- */
.tip { position: relative; }
.tip::after {
  content: attr(data-tip);
  position: absolute;
  top: calc(100% + 8px);
  left: 50%;
  z-index: 60;
  padding: 6px 10px;
  border-radius: var(--radius-sm);
  background: var(--tip-bg);
  color: var(--tip-fg);
  font: 500 var(--text-xs)/1.3 var(--font-sans);
  letter-spacing: 0;
  white-space: nowrap;
  box-shadow: var(--shadow-lg);
  opacity: 0;
  transform: translate(-50%, -3px);
  pointer-events: none;
  transition: opacity var(--dur-fast) var(--ease), transform var(--dur-fast) var(--ease);
}
.tip:hover::after, .tip:focus-visible::after, .tip:focus-within::after { opacity: 1; transform: translate(-50%, 0); }
.tip-end::after { left: auto; right: 0; transform: translate(0, -3px); }
.tip-end:hover::after, .tip-end:focus-visible::after, .tip-end:focus-within::after { transform: none; }
.tip-top::after { top: auto; bottom: calc(100% + 8px); transform: translate(-50%, 3px); }
.tip-top.tip-end::after { transform: translate(0, 3px); }

/* Buttons ------------------------------------------------------------------ */
button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  min-height: 34px;
  padding: 6px var(--space-3);
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  font-weight: 500;
  transition: background-color var(--dur) var(--ease), border-color var(--dur) var(--ease), color var(--dur) var(--ease), box-shadow var(--dur) var(--ease), transform var(--dur-fast) var(--ease), opacity var(--dur) var(--ease);
}
button:hover:not(:disabled) { background: var(--input); }
button:active:not(:disabled) { transform: scale(.98); }
button:disabled { opacity: .4; cursor: not-allowed; }

.text-button { min-height: 30px; padding: var(--space-1) var(--space-2); border: 0; background: transparent; color: var(--accent); font-size: var(--text-sm); font-weight: 600; border-radius: var(--radius-sm); }
.text-button:hover:not(:disabled) { background: var(--soft); }
.text-button.icon-only { width: 30px; padding: 0; }
.text-button.danger { color: var(--danger); }
.text-button.danger:hover:not(:disabled) { background: var(--danger-soft); }

.icon-button { width: 36px; height: 36px; min-height: 36px; padding: 0; border: 0; border-radius: var(--radius-pill); color: var(--muted); background: transparent; }
.icon-button:hover:not(:disabled) { color: var(--text); background: var(--input); }
.icon-button .icon { width: 20px; height: 20px; }
#settings.active::before { content: ''; position: absolute; top: 6px; right: 6px; width: 7px; height: 7px; border-radius: 50%; background: var(--accent); }

.primary {
  flex: 1;
  min-height: 46px;
  padding: var(--space-3) var(--space-4);
  background: var(--accent);
  color: var(--on-accent);
  border: 0;
  border-radius: var(--radius-md);
  font-size: var(--text-md);
  font-weight: 600;
}
.primary:hover:not(:disabled) { background: var(--accent-hover); color: var(--on-accent); }
.primary:disabled { opacity: .35; }

.spinner { width: 18px; height: 18px; flex: none; animation: spin .9s linear infinite; }
.spinner circle { stroke: currentColor; stroke-width: 2.5; fill: none; stroke-linecap: round; stroke-dasharray: 44 20; }
@keyframes spin { to { transform: rotate(360deg); } }

/* Inputs ------------------------------------------------------------------- */
select, input, textarea {
  width: 100%;
  min-width: 0;
  color: var(--text);
  background: var(--input);
  border: 1px solid transparent;
  border-radius: var(--radius-sm);
  padding: 0 var(--space-3);
  min-height: var(--control-h);
  transition: border-color var(--dur) var(--ease), background-color var(--dur) var(--ease);
}
select:hover:not(:disabled), input:hover:not(:disabled) { border-color: var(--border-strong); }
input[type="number"] { font-variant-numeric: tabular-nums; }
input:disabled, select:disabled, textarea:disabled { opacity: .5; cursor: not-allowed; }
input:invalid { border-color: var(--danger); color: var(--danger); }

/* ---------------------------------------------------------------------------
   Top bar
--------------------------------------------------------------------------- */
.topbar {
  height: var(--topbar-h);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--space-4);
  padding: 0 var(--space-5);
  background: var(--panel);
  border-bottom: 1px solid var(--border);
}
/* Every signed-in page carries the same title path: page icon + PeekLab / page name. */
.brand { display: flex; align-items: center; gap: 10px; margin: 0; font-size: var(--text-lg); font-weight: 600; letter-spacing: -.01em; line-height: 1.2; color: var(--text); white-space: nowrap; }
.brand img { width: 28px; height: 28px; border-radius: 8px; }
.brand .brand-icon { width: 28px; height: 28px; padding: 5px; border-radius: 8px; background: var(--accent); color: var(--on-accent); stroke-width: 2; }
.brand .brand-root { color: var(--text); text-decoration: none; }
.brand .brand-root:hover { color: var(--accent); }
.brand .brand-divider { font-size: var(--text-xl); font-weight: 400; color: var(--border-strong); margin: 0 2px; }
.brand .brand-label { font-size: var(--text-base); font-weight: 500; color: var(--muted); }
.toolbar { display: flex; align-items: center; gap: var(--space-2); min-width: 0; }
.model-picker { width: clamp(200px, 26vw, 360px); }
/* The toolbar reads left to right as work (manual, connection, model) → preference (theme) →
   management (prompt settings, users) → me (account). A hairline marks each seam. */
.toolbar > .group-start { margin-left: var(--space-3); }
.toolbar > .group-start::before { content: ''; position: absolute; left: calc(var(--space-2) * -1); top: 50%; margin-top: -9px; width: 1px; height: 18px; background: var(--border); }

.connection { display: inline-flex; align-items: center; gap: 6px; margin-right: var(--space-1); font-size: var(--text-xs); font-weight: 600; color: var(--muted); font-variant-numeric: tabular-nums; }
.connection::before { content: ''; width: 8px; height: 8px; border-radius: 50%; background: var(--muted); flex: none; transition: background-color var(--dur) var(--ease); }
.connection.ok { color: var(--success); }
.connection.ok::before { background: var(--success); }
.connection.error { color: var(--danger); }
.connection.error::before { background: var(--danger); }
.connection.pending::before { animation: pulse 1.4s ease-in-out infinite; }
@keyframes pulse { 0%, 100% { opacity: 1; } 50% { opacity: .3; } }

/* ---------------------------------------------------------------------------
   Layout: three equal panels, no page scroll on desktop
--------------------------------------------------------------------------- */
main { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: var(--space-3); height: calc(100dvh - var(--topbar-h)); padding: var(--space-3); }
.panel {
  min-height: 0;
  min-width: 0;
  display: flex;
  flex-direction: column;
  gap: var(--space-3);
  padding: var(--space-4) var(--space-5) var(--space-5);
  background: var(--panel);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-sm);
  overflow: hidden;
}

.panel-head { position: relative; display: flex; align-items: center; gap: var(--space-2); min-height: 32px; }
.panel h2 { font-size: var(--text-xl); font-weight: 700; letter-spacing: -.02em; line-height: 1.2; }
.chip { display: inline-block; max-width: 60%; padding: 3px 8px; border-radius: 6px; background: var(--input); color: var(--muted); font-size: var(--text-xs); font-weight: 500; }
.chip:empty { display: none; }
button.chip { border: 0; min-height: 0; line-height: 1.4; }
button.chip:disabled { opacity: 1; cursor: default; }
.chip.has-missing { color: var(--warn); background: rgba(255, 149, 0, .14); font-weight: 600; }
button.chip.has-missing:hover { background: rgba(255, 149, 0, .26); }
.fields { display: inline-flex; min-width: 0; max-width: 60%; }
.fields .chip { max-width: 100%; }
/* Popovers anchored to a panel head span the whole column instead of being clipped by the panel. */
.popover, #i18n-menu {
  position: absolute;
  top: calc(100% + 10px);
  left: 0;
  right: 0;
  z-index: 30;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: var(--space-5);
  box-shadow: var(--shadow-lg);
  text-align: left;
  animation: pop-in var(--dur) var(--ease);
  transform-origin: top center;
}
.popover .menu-head, #i18n-menu .menu-head { margin-bottom: var(--space-3); }
/* Reserve the scrollbar gutter so it never sits on top of the row buttons. */
#fields-list { max-height: 42vh; overflow-y: auto; scrollbar-gutter: stable; padding-right: var(--space-2); margin-right: calc(-1 * var(--space-2)); }
.user-role { padding: 1px 6px; border-radius: 4px; background: var(--input); color: var(--muted); font-size: 11px; font-weight: 600; text-transform: uppercase; }
.field-row { display: flex; flex-direction: column; gap: var(--space-1); padding: var(--space-3) 0; border-top: 1px solid var(--border); }
.field-row:first-child { border-top: 0; padding-top: 0; }
.field-head { display: flex; align-items: center; gap: var(--space-2); }
.field-row .icon-only { width: 26px; min-height: 26px; }
.field-row .text-button.icon-only:not(:disabled) { background: transparent; color: var(--danger); }
.field-row .text-button.icon-only:not(:disabled):hover { background: var(--danger-soft); }
.field-tag { margin-left: var(--space-2); padding: 1px 6px; border-radius: 4px; background: rgba(255, 149, 0, .14); color: var(--warn); font-size: 11px; font-weight: 600; }
.field-form { display: flex; flex-direction: column; gap: var(--space-2); margin-top: var(--space-3); padding-top: var(--space-3); border-top: 1px solid var(--border); }
.field-form input, .field-form select { min-height: 36px; font-size: var(--text-sm); }
.field-form textarea { display: block; width: 100%; resize: none; padding: var(--space-2) var(--space-3); border-radius: var(--radius-sm); border-color: transparent; font: var(--text-xs)/1.7 var(--font-mono); field-sizing: content; min-height: 2lh; max-height: 20vh; overflow: auto; }
.field-form textarea:focus-visible, .field-form input:focus-visible { outline: 0; border-color: var(--accent); }
#field-add:not(:disabled), #new-submit:not(:disabled), #user-add:not(:disabled) { background: var(--accent); color: var(--on-accent); }
#field-add:not(:disabled):hover, #new-submit:not(:disabled):hover, #user-add:not(:disabled):hover { background: var(--accent-hover); }
.field-q { flex: 1; min-width: 0; font-size: var(--text-sm); font-weight: 600; overflow: hidden; text-overflow: ellipsis; }
.field-row textarea { display: block; width: 100%; resize: none; padding: var(--space-2) var(--space-3); border-radius: var(--radius-sm); border-color: transparent; font: var(--text-xs)/1.7 var(--font-mono); field-sizing: content; min-height: 2lh; max-height: 22vh; overflow: auto; }
/* Border colour instead of an outline: the outline would be clipped by the scrolling list. */
.field-row textarea:focus-visible, #prefix:focus-visible, #suffix:focus-visible { outline: 0; border-color: var(--accent); }
.field-row .text-button:not(:disabled) { background: var(--accent); color: var(--on-accent); }
.field-row .text-button:not(:disabled):hover { background: var(--accent-hover); }
.fields-empty { padding: var(--space-2) 0; color: var(--muted); font-size: var(--text-sm); }
.creator { display: inline-flex; }
.state { font-size: var(--text-xs); font-weight: 600; color: var(--warn); }
.state:empty { display: none; }

/* Prompt picker ------------------------------------------------------------ */
.prompt-picker, .model-picker { position: relative; flex-shrink: 0; }
#prompt-select, #model-select {
  width: 100%;
  min-height: var(--control-h);
  justify-content: space-between;
  text-align: left;
  background: var(--input);
  border-color: transparent;
  font-weight: 600;
}
#prompt-select .icon, #model-select .icon { color: var(--muted); transition: transform var(--dur) var(--ease); }
#prompt-select-label, #model-select-label { flex: 1; min-width: 0; }
#prompt-select[aria-expanded="true"], #model-select[aria-expanded="true"] { border-color: var(--accent); }
#prompt-select[aria-expanded="true"] .icon, #model-select[aria-expanded="true"] .icon { transform: rotate(180deg); }
#prompt-menu, #model-menu {
  position: absolute;
  top: calc(var(--control-h) + 6px);
  left: 0;
  right: 0;
  z-index: 20;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: var(--space-3);
  box-shadow: var(--shadow-lg);
  animation: pop-in var(--dur) var(--ease);
  transform-origin: top center;
}
@keyframes pop-in { from { opacity: 0; transform: translateY(-4px) scale(.98); } to { opacity: 1; transform: none; } }
#prompt-search { min-height: 36px; font-size: var(--text-sm); }
#prompt-options { display: flex; flex-direction: column; gap: 2px; margin-top: var(--space-2); min-height: 120px; max-height: min(46vh, 420px); overflow-y: auto; font-size: var(--text-sm); color: var(--muted); }
#prompt-options button, #model-options button { flex-shrink: 0; justify-content: flex-start; min-height: 36px; text-align: left; font-size: var(--text-sm); font-weight: 500; border: 0; padding: var(--space-2); color: var(--text); }
#prompt-options button span, #model-options button span { overflow: hidden; text-overflow: ellipsis; }
#prompt-options button:hover, #model-options button:hover { background: var(--input); }
#prompt-options button[aria-selected="true"], #model-options button[aria-selected="true"] { background: var(--soft); color: var(--accent); }

/* The model list is short and lives in the toolbar: it hangs from the right edge, needs no search box
   and no minimum height, and names the service each model came from on the right of its row. */
#model-menu { left: auto; min-width: 100%; width: max-content; max-width: min(360px, calc(100vw - var(--space-4) * 2)); }
#model-options { display: flex; flex-direction: column; gap: 2px; max-height: min(46vh, 420px); overflow-y: auto; }
#model-options button { gap: var(--space-3); justify-content: space-between; }
#model-options .model-id { font-family: var(--font-mono); font-size: var(--text-xs); }
#model-options .model-source { flex: none; font-size: var(--text-xs); color: var(--muted); }
#model-options button[aria-selected="true"] .model-source { color: inherit; }
#model-options .empty-options { padding: var(--space-4); text-align: center; font-size: var(--text-sm); color: var(--muted); }
#prompt-options .empty-options { padding: var(--space-4); text-align: center; }
.menu-foot { display: flex; justify-content: space-between; align-items: center; gap: var(--space-2); margin-top: var(--space-2); padding-top: var(--space-2); border-top: 1px solid var(--border); }
.option-count { font-size: var(--text-xs); color: var(--muted); font-variant-numeric: tabular-nums; }

/* Prompt editor ------------------------------------------------------------ */
#save-prompt:not(:disabled), #suffix-save:not(:disabled) { background: var(--accent); color: var(--on-accent); }
#save-prompt:not(:disabled):hover, #suffix-save:not(:disabled):hover { background: var(--accent-hover); }
.prompt-content {
  flex: 1;
  min-height: 0;
  display: block;
  width: 100%;
  resize: none;
  background: var(--input);
  border: 1px solid transparent;
  border-radius: var(--radius-md);
  padding: var(--space-4);
  font: var(--text-sm)/1.75 var(--font-mono);
  overflow-y: auto;
  overflow-wrap: anywhere;
}
textarea.prompt-content:focus-visible { outline: 0; border-color: var(--accent); }
textarea::placeholder { color: var(--muted); }

/* Upload ------------------------------------------------------------------- */
.upload {
  flex: 1;
  min-height: 120px;
  position: relative;
  width: 100%;
  padding: var(--space-4);
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
  border: 1.5px dashed var(--border-strong);
  border-radius: var(--radius-lg);
  background: var(--input);
  color: var(--muted);
  font-weight: 500;
}
.upload:hover:not(:disabled) { border-color: var(--accent); background: var(--soft); color: var(--accent); }
.upload.has-image { border-style: solid; border-color: var(--border); }
.upload.drag { background: var(--soft); border-color: var(--accent); border-style: solid; color: var(--accent); }
.upload:disabled { opacity: 1; cursor: default; }
.upload img { position: absolute; inset: var(--space-2); width: calc(100% - 2 * var(--space-2)); height: calc(100% - 2 * var(--space-2)); object-fit: contain; border-radius: var(--radius-sm); }
.upload-placeholder { display: flex; flex-direction: column; align-items: center; gap: var(--space-3); font-size: var(--text-base); }

/* Parameters --------------------------------------------------------------- */
.params { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: var(--space-2); }
.params label { display: flex; flex-direction: column; gap: var(--space-1); font-size: var(--text-xs); font-weight: 500; color: var(--muted); }
.params label > span { display: inline-flex; align-items: center; gap: 4px; }
.params .help { width: 18px; height: 18px; }
.params .help .icon { width: 14px; height: 14px; }
.params input { padding: 0 var(--space-2); font-size: var(--text-md); font-weight: 600; text-align: center; color: var(--text); }
.submit-row { display: flex; gap: var(--space-2); margin-top: var(--space-4); }
.submit-row #cancel { flex: none; min-height: 46px; padding: 0 var(--space-4); border-radius: var(--radius-md); color: var(--danger); }

/* Result ------------------------------------------------------------------- */
.badge { display: inline-flex; align-items: center; gap: 7px; padding: 6px 12px; border-radius: var(--radius-pill); background: var(--input); color: var(--muted); font-size: var(--text-sm); font-weight: 600; font-variant-numeric: tabular-nums; transition: background-color var(--dur) var(--ease), color var(--dur) var(--ease); }
.badge::before { content: ''; width: 8px; height: 8px; border-radius: 50%; background: var(--border-strong); flex: none; }
.badge.busy { background: var(--soft); color: var(--accent); }
.badge.busy::before { background: var(--accent); animation: pulse 1.2s ease-in-out infinite; }
.badge.done { background: var(--success-soft); color: var(--success); }
.badge.done::before { background: var(--success); }
.badge.error { background: var(--danger-soft); color: var(--danger); }
.badge.error::before { background: var(--danger); }

.result-tools { display: flex; justify-content: space-between; align-items: center; gap: var(--space-2); }
.result-tools > div { display: inline-flex; gap: var(--space-1); }
.tabs { display: inline-flex; padding: 3px; border-radius: var(--radius-sm); background: var(--input); }
.tabs button { min-height: 28px; padding: 2px var(--space-3); background: transparent; border: 0; border-radius: 6px; font-size: var(--text-sm); font-weight: 500; color: var(--muted); }
.tabs button:hover:not(:disabled) { color: var(--text); background: transparent; }
.tabs button[aria-selected="true"] { background: var(--panel); color: var(--text); box-shadow: var(--shadow-sm); }

#result { flex: 1; min-height: 0; overflow: auto; display: flex; flex-direction: column; gap: 6px; }
#result[aria-busy="true"] { opacity: .6; }
.empty, .notice { flex: 1; display: flex; align-items: center; justify-content: center; gap: var(--space-2); color: var(--muted); font-size: var(--text-base); font-weight: 500; }
.notice.error { color: var(--danger); }

/* One row per field: label left, value right. Full text on hover. */
.result-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--space-4);
  flex-shrink: 0;
  min-height: 44px;
  padding: var(--space-2) var(--space-3);
  background: var(--input);
  border-radius: var(--radius-md);
  font-size: var(--text-base);
}
.result-key { flex: 1 1 auto; min-width: 0; display: flex; }
.result-key .k-zh { font-weight: 500; color: var(--text); overflow: hidden; text-overflow: ellipsis; }
.result-key .k-en { color: var(--muted); font: var(--text-sm)/1.4 var(--font-mono); overflow: hidden; text-overflow: ellipsis; }
.result-value { flex: 0 1 auto; max-width: 55%; min-width: 0; display: flex; justify-content: flex-end; text-align: right; font-weight: 600; font-size: var(--text-md); font-variant-numeric: tabular-nums; }
.result-value .v { overflow: hidden; text-overflow: ellipsis; }
.result-value.number { color: var(--accent); font-family: var(--font-mono); }
.result-value.boolean.v-true .v { color: var(--success); }
.result-value.boolean.v-false .v { color: var(--muted); }
.result-value.na .v { color: var(--muted); font-weight: 500; }
.raw-text { margin: 0; padding: var(--space-3) var(--space-4); background: var(--input); border-radius: var(--radius-md); font: var(--text-xs)/1.7 var(--font-mono); color: var(--text); overflow: auto; }

.result-meta { display: flex; gap: var(--space-2); min-height: 28px; }
.meta-chip { display: inline-flex; align-items: center; gap: 4px; max-width: 50%; padding: 4px 10px; border-radius: var(--radius-pill); background: var(--input); color: var(--muted); font-size: var(--text-xs); font-weight: 500; font-variant-numeric: tabular-nums; }
.meta-chip strong { color: var(--text); font-weight: 700; font-size: var(--text-sm); overflow: hidden; text-overflow: ellipsis; }

#result, #prompt-options, textarea, .raw-text { scrollbar-width: thin; scrollbar-color: var(--border-strong) transparent; }

/* Translation editor: anchored to the result panel head, opened from a row's pencil. -------- */
.row-edit { flex: none; width: 26px; height: 26px; min-height: 0; padding: 0; border: 0; border-radius: 6px; background: transparent; color: var(--muted); opacity: 0; transition: opacity var(--dur-fast) var(--ease), background-color var(--dur) var(--ease); }
.result-row:hover .row-edit, .row-edit:focus-visible { opacity: 1; }
.row-edit:hover { background: var(--soft); color: var(--accent); }
.row-edit .icon { width: 14px; height: 14px; }
@media (hover: none) { .row-edit { opacity: 1; } }
.translator-key { display: block; margin-bottom: var(--space-3); color: var(--muted); font: var(--text-sm)/1.4 var(--font-mono); overflow: hidden; text-overflow: ellipsis; }
.form-row { display: grid; grid-template-columns: minmax(72px, auto) minmax(0, 1fr); align-items: center; gap: var(--space-3); margin-top: var(--space-2); font-size: var(--text-sm); font-weight: 500; color: var(--muted); }
.form-row > span { overflow: hidden; text-overflow: ellipsis; }
.form-row input { min-height: 36px; font-size: var(--text-base); color: var(--text); }
.form-row input:focus-visible { outline: 0; border-color: var(--accent); }
#i18n-save:not(:disabled) { background: var(--accent); color: var(--on-accent); }
#i18n-save:not(:disabled):hover { background: var(--accent-hover); }

/* Settings popover ----------------------------------------------------------- */
.settings { position: relative; }
#settings-menu {
  position: absolute;
  top: calc(36px + 10px);
  right: 0;
  width: min(480px, calc(100vw - 32px));
  z-index: 30;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: var(--space-4) var(--space-5);
  box-shadow: var(--shadow-lg);
  text-align: left;
  animation: pop-in var(--dur) var(--ease);
  transform-origin: top right;
}
.menu-head { display: flex; align-items: center; justify-content: space-between; gap: var(--space-2); margin-bottom: var(--space-2); }
#settings-menu h2 { font-size: var(--text-md); font-weight: 600; }
.account-info { margin-bottom: var(--space-2); font-size: var(--text-sm); color: var(--muted); white-space: normal; }
.help { display: inline-flex; align-items: center; justify-content: center; width: 24px; height: 24px; border-radius: 50%; color: var(--muted); cursor: help; }
.help:hover { color: var(--accent); background: var(--soft); }
#prefix, #suffix { display: block; width: 100%; resize: none; padding: var(--space-3); border-radius: var(--radius-md); border-color: transparent; font: var(--text-xs)/1.7 var(--font-mono); field-sizing: content; min-height: 3lh; max-height: 28vh; overflow: auto; }
.settings-label { display: block; margin: var(--space-3) 0 var(--space-1); font-size: var(--text-xs); font-weight: 600; color: var(--muted); }
.settings-label:first-of-type { margin-top: 0; }
.settings-actions { display: flex; justify-content: space-between; align-items: center; gap: var(--space-2); margin-top: var(--space-3); min-height: 30px; }
.settings-actions > div { display: inline-flex; gap: var(--space-1); }

/* Toast --------------------------------------------------------------------- */
#toast {
  position: fixed;
  bottom: var(--space-5);
  left: 50%;
  transform: translateX(-50%);
  z-index: 100;
  max-width: min(80vw, 560px);
  padding: var(--space-3) var(--space-4);
  border-radius: var(--radius-md);
  background: var(--tip-bg);
  color: var(--tip-fg);
  font-size: var(--text-sm);
  font-weight: 500;
  line-height: 1.5;
  white-space: pre-line;
  box-shadow: var(--shadow-lg);
  animation: toast-in var(--dur) var(--ease);
}
@keyframes toast-in { from { opacity: 0; transform: translate(-50%, 8px); } to { opacity: 1; transform: translate(-50%, 0); } }

.theme-moon { display: none; }
body.dark .theme-moon { display: block; }
body.dark .theme-sun { display: none; }
.clipboard-fallback { position: fixed; left: -9999px; }

/* ---------------------------------------------------------------------------
   Responsive
--------------------------------------------------------------------------- */
@media (max-width: 1180px) {
  .panel { padding: var(--space-3) var(--space-4) var(--space-4); }
  .topbar { padding: 0 var(--space-4); }
  .model-picker { width: clamp(160px, 24vw, 300px); }
}

@media (max-height: 720px) {
  :root { --topbar-h: 52px; }
  main { padding: var(--space-2); gap: var(--space-2); }
  .panel { gap: var(--space-2); padding: var(--space-3) var(--space-4); }
  .panel h2 { font-size: var(--text-lg); }
  .result-row { min-height: 38px; padding: 6px var(--space-3); }
}

/* Below tablet width the three columns stack and the page scrolls. */
@media (max-width: 860px) {
  body { overflow: auto; height: auto; min-height: 100dvh; }
  .popover, #i18n-menu { position: fixed; top: calc(var(--topbar-h) + 8px); right: var(--space-4); left: var(--space-4); width: auto; }
  main { display: flex; flex-direction: column; height: auto; }
  .panel { min-height: calc(100dvh - var(--topbar-h) - 2 * var(--space-3)); }
  .topbar { position: sticky; top: 0; z-index: 40; }
  .brand span { display: none; }
  .manual-link { display: none; }
  .connection { display: none; }
  .toolbar { flex: 1; justify-content: flex-end; }
  .model-picker { flex: 1; width: auto; }
  #settings-menu { position: fixed; top: calc(var(--topbar-h) + 8px); right: var(--space-4); left: var(--space-4); width: auto; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: .01ms !important; animation-iteration-count: 1 !important; transition-duration: .01ms !important; }
  .spinner { animation: none; }
  .spinner circle { stroke-dasharray: 30 34; }
}

/* Shared workspace navigation; new tools can join the same icon/label list.
   Every icon (toggle, nav items, avatar) sits on one fixed column, --sidebar-icon-x from the sidebar's
   left edge, in both states: collapsing only removes the labels and narrows the width, nothing moves. */
body.workspace { --sidebar-w: 216px; --sidebar-icon-x: 23px; padding-left: var(--sidebar-w); }
body.workspace.sidebar-collapsed { --sidebar-w: 64px; }
.sidebar {
  position: fixed; inset: 0 auto 0 0; z-index: 45;
  width: var(--sidebar-w); display: flex; flex-direction: column;
  padding: 0 var(--space-2) var(--space-3);
  background: var(--panel); border-right: 1px solid var(--border);
}
/* Logo is 28px: offset it so its centre sits on the icon column (icon centre = icon-x + 9px). */
.sidebar-brand { display: flex; align-items: center; gap: 10px; min-height: var(--topbar-h); padding: 0 0 0 calc(var(--sidebar-icon-x) + 9px - 14px - var(--space-2)); border-bottom: 1px solid var(--border); font-weight: 600; font-size: var(--text-lg); white-space: nowrap; overflow: hidden; }
.sidebar-brand img { flex: none; width: 28px; height: 28px; border-radius: 8px; }
.sidebar-brand .nav-label { flex: 1; min-width: 0; }
/* Expanded: toggle hugs the right end of the logo row. Collapsed: the row shows only the toggle, centred on the icon column. */
.sidebar-toggle { flex: none; margin-left: auto; }
.sidebar-collapsed .sidebar-brand { padding: 0; justify-content: center; }
.sidebar-collapsed .sidebar-brand img { display: none; }
.sidebar-collapsed .sidebar-toggle { margin: 0; }
.sidebar nav { padding-top: var(--space-3); flex: 1; min-height: 0; overflow-y: auto; }
.sidebar .nav-item {
  position: relative; display: flex; align-items: center; justify-content: flex-start;
  width: 100%; min-height: 44px; gap: var(--space-3); padding: var(--space-3) var(--space-3) var(--space-3) calc(var(--sidebar-icon-x) - var(--space-2) - 1px);
  margin-bottom: var(--space-1); border: 1px solid transparent; overflow: hidden;
  border-radius: var(--radius-sm); background: transparent; color: var(--muted);
  font-size: var(--text-sm); font-weight: 500; text-decoration: none;
}
.sidebar .nav-item:hover { background: var(--input); color: var(--text); }
.sidebar .nav-item[aria-current="page"], .sidebar .nav-item[aria-expanded="true"]:not(.sidebar-toggle) { background: var(--soft); color: var(--accent); font-weight: 600; }
.sidebar .nav-item[aria-current="page"]::before { content: ''; position: absolute; left: 0; top: 12px; bottom: 12px; width: 3px; border-radius: 2px; background: var(--accent); }
.nav-divider { margin: var(--space-4) var(--space-3); border-top: 1px solid var(--border); }
.sidebar-footer { padding-top: var(--space-3); border-top: 1px solid var(--border); }
.sidebar-collapsed .sidebar .nav-label { display: none; }
.sidebar-collapsed .nav-divider { margin: var(--space-4) var(--space-1); }
@media (min-width: 861px) and (max-width: 1180px) {
  .workspace main { grid-template-columns: repeat(2, minmax(0, 1fr)); height: auto; }
  .workspace .result-panel { grid-column: 1 / -1; min-height: 360px; }
  body.workspace { overflow: auto; }
}
@media (max-width: 860px) {
  body.workspace { --sidebar-w: 64px; }
  body.workspace:not(.sidebar-collapsed) .sidebar { width: min(260px, calc(100vw - 32px)); box-shadow: var(--shadow-lg); }
  .workspace .topbar { gap: var(--space-2); padding: 0 var(--space-2); }
  .workspace .topbar .brand { display: none; }
  .workspace .toolbar { gap: var(--space-1); }
  .workspace .toolbar > .group-start { margin-left: var(--space-1); }
  .workspace .toolbar > .group-start::before { display: none; }
  .sidebar .settings-actions { align-items: flex-start; flex-wrap: wrap; }
  .sidebar .help::after { max-width: calc(100vw - 112px); white-space: normal; }
}
@media (max-width: 480px) {
  .workspace .model-picker { min-width: 0; }
  .workspace .panel-head, .workspace .result-tools { flex-wrap: wrap; }
  .workspace .params { grid-template-columns: 1fr; }
  .workspace .params label { flex-direction: row; align-items: center; justify-content: space-between; }
  .workspace .params input { width: 80px; }
  .workspace .empty, .workspace .notice { white-space: normal; text-align: center; }
}

/* Account identity stays at the bottom of every workspace page. */
.avatar { display: inline-flex; align-items: center; justify-content: center; flex: none; overflow: hidden; border-radius: 50%; background: var(--soft); color: var(--accent); }
.avatar img { width: 100%; height: 100%; object-fit: cover; }
.avatar > span { display: flex; align-items: center; justify-content: center; }
.sidebar-avatar { width: 36px; height: 36px; }
/* Avatar is 36px; centre it on the icon column (icon centre = icon-x + 9px). */
.sidebar .account-link { padding: var(--space-2) var(--space-2) var(--space-2) calc(var(--sidebar-icon-x) + 9px - 18px - var(--space-2) - 1px); }
/* Fixed line heights so a row is exactly as tall with or without its label (icons 18px, avatar 36px). */
.sidebar .nav-label { line-height: 18px; overflow: hidden; text-overflow: ellipsis; }
.sidebar .account-label { min-width: 0; display: flex; flex-direction: column; gap: 2px; }
.account-label strong { overflow: hidden; text-overflow: ellipsis; font-weight: 600; line-height: 18px; }
.account-label small { font-size: var(--text-xs); color: var(--muted); line-height: 14px; }
.sidebar-collapsed .sidebar .account-label { display: none; }
.sidebar-brand { flex: none; }
.sidebar-footer { flex: none; }
