:root {
  --canvas: #f4f2ee;
  --surface: #ffffff;
  --surface-2: #f7f6f3;
  --ink: #16202a;
  --ink-soft: #46525e;
  --muted: #7c8794;
  --line: #e8e4dd;
  --line-strong: #d8d3c9;
  --accent: #3159c9;
  --accent-hover: #2a4fb5;
  --accent-soft: #eef2fd;
  --accent-line: #cdd9f7;
  --accent-ink: #ffffff;
  --danger: #a8342a;
  --danger-soft: #fdf2f0;
  --radius-card: 16px;
  --radius-control: 10px;
  --radius-pill: 999px;
  --control-h: 38px;
  --control-font: .94rem;
  --shadow-sm: 0 1px 2px rgba(22, 32, 42, .04), 0 1px 3px rgba(22, 32, 42, .04);
  --shadow-md: 0 6px 16px rgba(22, 32, 42, .08), 0 1px 3px rgba(22, 32, 42, .05);
  --ease: cubic-bezier(.2, .7, .3, 1);
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--canvas);
  color: var(--ink);
  font: 16px/1.55 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", system-ui, sans-serif;
  -webkit-font-smoothing: antialiased;
  -webkit-text-size-adjust: 100%;
}

.hidden { display: none !important; }
.muted { color: var(--muted); font-size: .88rem; }
.error { color: var(--danger); font-size: .9rem; }

h1, h2 { margin: 0; font-weight: 640; letter-spacing: -.015em; }
h2 { font-size: 1.1rem; }

/* ---------- controls ---------- */

button svg, summary.icon svg {
  width: 18px;
  height: 18px;
  flex: none;
  display: block;
}
button.icon svg, summary.icon svg { width: 20px; height: 20px; }
.menu-panel button svg { width: 17px; height: 17px; color: currentColor; opacity: .85; }

button, summary.icon {
  font: inherit;
  font-size: var(--control-font);
  font-weight: 520;
  line-height: 1;
  height: var(--control-h);
  padding: 0 14px;
  border-radius: var(--radius-control);
  border: 1px solid transparent;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 7px;
  white-space: nowrap;
  transition: background-color .15s var(--ease), border-color .15s var(--ease),
              color .15s var(--ease), opacity .15s var(--ease);
}

button.primary {
  background: var(--accent);
  color: var(--accent-ink);
  border-radius: var(--radius-pill);
  padding: 0 18px;
  min-width: 96px;
}
button.primary:hover { background: var(--accent-hover); }

button.ghost {
  background: var(--surface);
  border-color: var(--line-strong);
  color: var(--ink-soft);
}
button.ghost:hover { border-color: var(--muted); color: var(--ink); }

button.quiet {
  background: transparent;
  border-color: transparent;
  color: var(--muted);
  padding: 0 8px;
}
button.quiet:hover { color: var(--ink); background: var(--surface-2); }
button.quiet.danger { color: var(--danger); }
button.quiet.danger:hover { background: var(--danger-soft); }

button.icon {
  width: var(--control-h);
  min-width: var(--control-h);
  padding: 0;
  background: transparent;
  border-color: transparent;
  color: var(--ink-soft);
  border-radius: var(--radius-control);
  font-size: 1.05rem;
}
button.icon:hover { background: var(--surface-2); color: var(--ink); }

button:focus-visible, summary:focus-visible, select:focus-visible, input:focus-visible,
[role="button"]:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

button[disabled] { opacity: .55; cursor: default; }
button.is-busy, summary.is-busy { opacity: .6; cursor: progress; }

/* ---------- menus ---------- */

.menu { position: relative; }
.menu > summary { list-style: none; cursor: pointer; }
.menu > summary::-webkit-details-marker { display: none; }
.menu-panel {
  position: absolute;
  top: calc(100% + 6px);
  right: 0;
  left: auto;
  z-index: 40;
  width: max-content;
  min-width: 0;
  max-width: min(280px, calc(100vw - 20px));
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 12px;
  box-shadow: var(--shadow-md);
  padding: 6px;
  display: grid;
  gap: 2px;
}
.menu-panel button {
  width: 100%;
  height: 40px;
  padding: 0 12px;
  gap: 10px;
  justify-content: flex-start;
  text-align: left;
  white-space: nowrap;
  font-size: .92rem;
  background: transparent;
  border-color: transparent;
  color: var(--ink-soft);
  border-radius: 9px;
}
.menu-panel button:hover { background: var(--surface-2); color: var(--ink); }

.menu-sep { height: 1px; background: var(--line); margin: 4px 2px; }

.sheet {
  position: fixed;
  inset: 0;
  z-index: 50;
  background: rgba(22, 32, 42, .38);
  display: flex;
  align-items: flex-end;
  justify-content: center;
  padding: 0;
}
.sheet-card {
  background: var(--surface);
  width: 100%;
  max-width: 560px;
  max-height: 88vh;
  border-radius: 18px 18px 0 0;
  display: grid;
  grid-template-rows: auto 1fr auto;
  box-shadow: 0 -8px 28px rgba(22, 32, 42, .18);
}
.sheet-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding: 14px 16px;
  border-bottom: 1px solid var(--line);
}
.sheet-head h2 { font-size: 1.05rem; }
.sheet-body {
  overflow-y: auto;
  overflow-x: hidden;
  min-width: 0;
  padding: 16px;
  padding-bottom: calc(16px + env(safe-area-inset-bottom, 0px));
}
.sheet-body #params-note { margin: 12px 0 0; }
.sheet-foot {
  display: grid;
  gap: 10px;
  padding: 12px 16px calc(12px + env(safe-area-inset-bottom, 0px));
  border-top: 1px solid var(--line);
  background: var(--surface-2);
}
.sheet-foot-row { display: flex; gap: 8px; justify-content: flex-end; }
.sheet-foot .wide { justify-content: center; width: 100%; }

@media (min-width: 620px) {
  .sheet { align-items: center; padding: 24px; }
  .sheet-card { border-radius: 18px; max-height: 80vh; }
}

/* ---------- top bar ---------- */

.topbar {
  position: sticky;
  top: 0;
  z-index: 30;
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px 14px;
  padding-top: calc(10px + env(safe-area-inset-top, 0px));
  background: rgba(255, 255, 255, .88);
  backdrop-filter: saturate(150%) blur(10px);
  border-bottom: 1px solid var(--line);
}

.brand { display: flex; align-items: center; gap: 8px; min-width: 0; flex: 1 1 auto; }
.brand-mark {
  width: 24px; height: 24px;
  border-radius: 7px;
  background: linear-gradient(150deg, var(--accent), #5b83ea);
  position: relative;
  flex: none;
}
.brand-mark::before, .brand-mark::after {
  content: "";
  position: absolute;
  background: #fff;
  border-radius: 3px;
}
.brand-mark::before { width: 8px; height: 3px; left: 6px; top: 8px; }
.brand-mark::after { width: 5px; height: 3px; left: 6px; top: 14px; opacity: .7; }

.topbar h1 {
  font-size: 1rem;
  font-weight: 620;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  min-width: 0;
}
.topbar-actions { margin-left: auto; display: flex; align-items: center; gap: 8px; flex: 0 0 auto; }

/* ---------- layout ---------- */

main {
  padding: 18px 14px calc(32px + env(safe-area-inset-bottom, 0px));
  max-width: 900px;
  margin: 0 auto;
}

.surface {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius-card);
  padding: 16px;
  box-shadow: var(--shadow-sm);
}

.empty {
  display: grid;
  justify-items: center;
  gap: 8px;
  text-align: center;
  padding: 42px 18px;
  background: var(--surface);
  border: 1px dashed var(--line-strong);
  border-radius: var(--radius-card);
  color: var(--muted);
  font-size: .92rem;
}
.empty .bubbles { display: flex; align-items: flex-end; gap: 6px; margin-bottom: 4px; }
.empty .bubbles i {
  display: block; width: 30px; height: 22px;
  border-radius: 8px; border: 1.5px solid var(--line-strong);
  background: var(--surface-2);
}
.empty .bubbles i:last-child { width: 22px; height: 16px; background: var(--accent-soft); border-color: var(--accent-line); }
.empty strong { color: var(--ink); font-weight: 620; }

/* ---------- dialog list ---------- */

.dialog-list { list-style: none; margin: 0; padding: 0; display: grid; gap: 10px; }
.dialog-list li {
  position: relative;
  display: flex;
  align-items: center;
  gap: 8px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius-card);
  padding: 10px 8px 10px 14px;
  box-shadow: var(--shadow-sm);
  transition: border-color .15s var(--ease), box-shadow .15s var(--ease);
}
.dialog-list li:hover { border-color: var(--line-strong); box-shadow: var(--shadow-md); }
.card-open {
  flex: 1 1 auto;
  min-width: 0;
  height: auto;
  padding: 4px 0;
  background: transparent;
  border-color: transparent;
  display: grid;
  gap: 2px;
  justify-items: start;
  text-align: left;
  border-radius: 10px;
}
.card-open:hover { background: transparent; }
.card-open, .card-open .title, .card-open .chips { white-space: normal; }
.card-open .title { font-weight: 620; letter-spacing: -.01em; }
.card-open .chevron { color: var(--muted); font-size: 1.1rem; line-height: 1; }
.row-loading { list-style: none; color: var(--muted); font-size: .92rem; padding: 14px; }

.chips { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 4px; }
.chip {
  font-size: .76rem;
  color: var(--ink-soft);
  background: var(--surface-2);
  border: 1px solid var(--line);
  border-radius: var(--radius-pill);
  padding: 2px 9px;
  white-space: nowrap;
}
.chip.accent { background: var(--accent-soft); border-color: var(--accent-line); color: #27419a; }

/* ---------- parameter form ---------- */

.create-grid { display: grid; gap: 14px 16px; grid-template-columns: 1fr; min-width: 0; }
.create-grid select, .create-grid input { min-width: 0; max-width: 100%; }
.field { display: block; min-width: 0; }
.field > span {
  display: block;
  font-size: .74rem;
  text-transform: uppercase;
  letter-spacing: .07em;
  color: var(--muted);
  margin-bottom: 5px;
  font-weight: 620;
}
.field > span b { color: var(--ink); font-weight: 640; letter-spacing: 0; }
.field select, .field input[type="password"] {
  width: 100%;
  height: 42px;
  padding: 0 12px;
  border: 1px solid var(--line-strong);
  border-radius: var(--radius-control);
  background: var(--surface);
  font: inherit;
  font-size: .95rem;
  color: var(--ink);
}
.field select:hover { border-color: var(--muted); }
.field input[type="password"] { appearance: none; background-image: none; }
.field select {
  appearance: none;
  background-image: linear-gradient(45deg, transparent 50%, var(--muted) 50%), linear-gradient(135deg, var(--muted) 50%, transparent 50%);
  background-position: calc(100% - 17px) 50%, calc(100% - 12px) 50%;
  background-size: 5px 5px, 5px 5px;
  background-repeat: no-repeat;
}
.field.range input[type="range"] { width: 100%; height: 42px; appearance: none; background: transparent; }
.field.range input[type="range"]::-webkit-slider-runnable-track { height: 6px; border-radius: var(--radius-pill); background: var(--line); }
.field.range input[type="range"]::-webkit-slider-thumb {
  appearance: none; width: 20px; height: 20px; margin-top: -7px;
  border-radius: 50%; background: var(--surface); border: 2px solid var(--accent);
}
.field.range input[type="range"]::-moz-range-track { height: 6px; border-radius: var(--radius-pill); background: var(--line); }
.field.range input[type="range"]::-moz-range-thumb { width: 18px; height: 18px; border-radius: 50%; background: var(--surface); border: 2px solid var(--accent); }

.create-actions {
  display: flex;
  gap: 8px;
  margin-top: 16px;
  flex-wrap: wrap;
  align-items: center;
}
.create-actions .primary { margin-left: auto; }

/* ---------- turns and options ---------- */

.turns { display: grid; gap: 10px; margin: 14px 0 4px; }
.turn {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius-card);
  padding: 11px 14px;
  box-shadow: var(--shadow-sm);
  max-width: 88%;
  justify-self: start;
}
.intro {
  background: var(--surface-2);
  border: 1px solid var(--line);
  border-radius: var(--radius-card);
  padding: 11px 14px;
  margin: 14px 0 0;
  font-size: .92rem;
  line-height: 1.45;
  color: var(--ink-soft);
}
.turn.you { justify-self: end; background: var(--accent-soft); border-color: var(--accent-line); }
.turn.thinking {
  justify-self: start;
  display: flex;
  align-items: center;
  justify-content: flex-start;
  gap: 8px;
  color: var(--muted);
  font-size: 1rem;
  line-height: 1.55;
  padding: 17.5px 14px;
}
.turn.thinking > span:first-child {
  text-box: trim-both cap alphabetic;
}
.turn.thinking .dots { display: inline-flex; gap: 4px; }
.turn.thinking .dots i {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: currentColor;
  animation: think 1.1s ease-in-out infinite;
}
.turn.thinking .dots i:nth-child(2) { animation-delay: .18s; }
.turn.thinking .dots i:nth-child(3) { animation-delay: .36s; }
@keyframes think {
  0%, 100% { opacity: .25; transform: translateY(0); }
  50% { opacity: 1; transform: translateY(-2px); }
}

.morph-ghost {
  display: flex;
  align-items: center;
  padding: 10px 14px;
  border: 1px solid var(--line);
  border-radius: var(--radius-card);
  box-shadow: var(--shadow-sm);
  font: inherit;
  font-size: 1rem;
  line-height: 1.4;
  white-space: normal;
  text-align: left;
  overflow: hidden;
}
.options:empty::before { content: none; }

.options button:disabled { opacity: .5; }
.options button.chosen {
  opacity: 1;
  border-color: var(--accent-line);
  background: var(--accent-soft);
  color: var(--ink);
}
.options button.chosen::after {
  content: "";
  width: 16px;
  height: 16px;
  flex: none;
  border-radius: 50%;
  background: var(--accent) url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24'%3E%3Cpath d='M5 13l4 4 10-10' fill='none' stroke='white' stroke-width='3' stroke-linecap='round' stroke-linejoin='round'/%3E%3C/svg%3E") center/12px no-repeat;
}
.options button.chosen.busy::after {
  background: none;
  border-width: 2px;
  border-style: solid;
  border-color: currentColor;
  border-top-color: transparent;
  color: var(--accent);
  animation: opt-spin .7s linear infinite;
}
@keyframes opt-spin { to { transform: rotate(360deg); } }

.options { display: grid; gap: 8px; margin-top: 10px; counter-reset: opt; }
.options-caption {
  font-size: .72rem;
  text-transform: uppercase;
  letter-spacing: .09em;
  color: var(--muted);
  font-weight: 620;
  margin: 12px 0 6px;
}
.options-caption.hidden { display: none; }
.options button {
  white-space: normal;
  height: auto;
  min-height: 50px;
  font-size: 1rem;
  font-weight: 400;
  display: flex;
  align-items: center;
  gap: 12px;
  text-align: left;
  background: var(--surface);
  border: 1px solid var(--line-strong);
  border-radius: var(--radius-card);
  padding: 13px 16px;
  box-shadow: var(--shadow-sm);
  color: var(--ink);
  justify-content: flex-start;
}
.options button .num {
  flex: none;
  width: 22px; height: 22px;
  border-radius: 50%;
  background: var(--accent-soft);
  border: 1px solid var(--accent-line);
  color: #27419a;
  font-size: .76rem;
  font-weight: 640;
  display: grid;
  place-items: center;
}
.options button:hover { border-color: var(--accent); box-shadow: var(--shadow-md); }

/* ---------- panels, progress, errors ---------- */

.panel {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius-card);
  padding: 12px 16px;
  margin: 14px 0;
  box-shadow: var(--shadow-sm);
}
.panel > summary {
  white-space: normal;
  cursor: pointer;
  font-weight: 620;
  font-size: .98rem;
  min-height: 42px;
  display: flex;
  align-items: center;
  gap: 8px;
  list-style: none;
}
.panel > summary::-webkit-details-marker { display: none; }
.panel > summary::after {
  content: "";
  margin-left: auto;
  width: 8px; height: 8px;
  border-right: 2px solid var(--muted);
  border-bottom: 2px solid var(--muted);
  transform: rotate(45deg);
  transition: transform .18s var(--ease);
}
.panel[open] > summary::after { transform: rotate(-135deg); }
.panel[open] > summary { margin-bottom: 14px; border-bottom: 1px solid var(--line); padding-bottom: 10px; }

.error-card {
  background: var(--danger-soft);
  border: 1px solid #edcfca;
  border-radius: var(--radius-card);
  padding: 14px 16px;
  margin: 14px 0;
  display: grid;
  gap: 10px;
  justify-items: start;
}
.error-card p { margin: 0; color: var(--danger); font-size: .92rem; }

.dialog-footer { display: flex; justify-content: center; margin: 22px 0 4px; }

.boot {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 50vh;
  color: var(--muted);
  font-size: .95rem;
}

body:not(.booting) .boot { display: none; }
body.booting main { visibility: hidden; }
body.booting .topbar { visibility: hidden; }

.busy-strip {
  position: fixed;
  top: 0; left: 0; right: 0;
  height: 2px;
  z-index: 60;
  background: linear-gradient(90deg, transparent, var(--accent), transparent);
  background-size: 40% 100%;
  background-repeat: no-repeat;
  animation: sweep 1.1s linear infinite;
  pointer-events: none;
}
@keyframes sweep {
  0% { background-position: -40% 0; }
  100% { background-position: 140% 0; }
}

.toast {
  position: fixed;
  left: 50%;
  bottom: calc(20px + env(safe-area-inset-bottom, 0px));
  transform: translate(-50%, 8px);
  background: var(--ink);
  color: #fff;
  padding: 10px 16px;
  border-radius: var(--radius-pill);
  font-size: .88rem;
  max-width: 90vw;
  box-shadow: var(--shadow-md);
  opacity: 0;
  transition: opacity .18s var(--ease), transform .18s var(--ease);
  pointer-events: none;
}
.toast:not(.hidden) { opacity: 1; transform: translate(-50%, 0); }

/* ---------- login ---------- */

.login-body {
  display: grid;
  place-items: center;
  min-height: 100vh;
  padding: 20px;
  background:
    radial-gradient(1100px 560px at 50% -10%, #e9edf9 0%, transparent 60%),
    var(--canvas);
}
.login-card {
  width: 100%;
  max-width: 360px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 18px;
  padding: 26px 24px;
  display: grid;
  gap: 16px;
  box-shadow: var(--shadow-md);
}
.login-card h1 { font-size: 1.22rem; }
.login-card .eyebrow { font-size: .72rem; text-transform: uppercase; letter-spacing: .1em; color: var(--muted); font-weight: 620; }
.login-card .brand-mark { width: 32px; height: 32px; border-radius: 9px; }
.login-card .brand-mark::before { width: 12px; height: 3px; left: 7px; top: 11px; }
.login-card .brand-mark::after { width: 7px; height: 3px; left: 7px; top: 18px; }
.login-card button.primary { width: 100%; border-radius: var(--radius-control); min-width: 0; height: 44px; }

/* ---------- responsive ---------- */

@media (max-width: 560px), (pointer: coarse) {
  :root { --control-h: 44px; --control-font: .96rem; }
  .topbar .primary { min-width: 92px; }
  button.icon, summary.icon { width: 44px; min-width: 44px; font-size: 1.15rem; }
  .menu-panel button { height: 44px; }
  .field select, .field input[type="password"] { height: 46px; }
  .field.range input[type="range"] { height: 46px; }
  .field.range input[type="range"]::-webkit-slider-thumb { width: 24px; height: 24px; margin-top: -9px; }
  .panel > summary { min-height: 46px; }
  .dialog-footer { margin: 18px 0 4px; }
  #btn-delete-dialog { height: 44px; }
}

@media (max-width: 420px) {
  .brand-mark { display: none; }
  .topbar { gap: 6px; }
  .topbar-actions { gap: 4px; }
}

@media (min-width: 760px) {
  main { padding: 28px 24px 48px; }
  .create-grid { grid-template-columns: 1fr 1fr; }
  .topbar { padding: 12px 22px; }
}

@media (prefers-reduced-motion: reduce) {
  * { transition: none !important; animation: none !important; }
}
