:root {
  --bg: #f5f3ee;
  --surface: #ffffff;
  --surface-2: #faf9f6;
  --ink: #1c1f24;
  --muted: #666b73;
  --line: #e4e1d9;
  --accent: #0f6f68;
  --accent-ink: #ffffff;
  --accent-soft: #e3f1ef;
  --danger: #b3261e;
  --shadow: 0 1px 2px rgba(20, 22, 26, 0.05), 0 8px 24px rgba(20, 22, 26, 0.06);
  --radius: 18px;
  --stage-1: #c9d2d0;
  --stage-2: #a9cdc8;
  --stage-3: #7fbdb5;
  --stage-4: #4fa49b;
  --stage-5: #278a80;
  --stage-6: #0f6f68;
  color-scheme: light;
}

:root[data-theme="dark"] {
  --bg: #121416;
  --surface: #1b1e21;
  --surface-2: #17191c;
  --ink: #eceae4;
  --muted: #9aa0a8;
  --line: #2a2e33;
  --accent: #4fb3a9;
  --accent-ink: #08201d;
  --accent-soft: #17302d;
  --danger: #f2b8b5;
  --shadow: 0 1px 2px rgba(0, 0, 0, 0.4), 0 8px 24px rgba(0, 0, 0, 0.35);
  --stage-1: #34403e;
  --stage-2: #2f5550;
  --stage-3: #2f6e67;
  --stage-4: #3a8a81;
  --stage-5: #4fa49b;
  --stage-6: #6fc4ba;
  color-scheme: dark;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #121416;
    --surface: #1b1e21;
    --surface-2: #17191c;
    --ink: #eceae4;
    --muted: #9aa0a8;
    --line: #2a2e33;
    --accent: #4fb3a9;
    --accent-ink: #08201d;
    --accent-soft: #17302d;
    --danger: #f2b8b5;
    --shadow: 0 1px 2px rgba(0, 0, 0, 0.4), 0 8px 24px rgba(0, 0, 0, 0.35);
    --stage-1: #34403e;
    --stage-2: #2f5550;
    --stage-3: #2f6e67;
    --stage-4: #3a8a81;
    --stage-5: #4fa49b;
    --stage-6: #6fc4ba;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }

[hidden] { display: none !important; }

html, body { height: 100%; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font: 16px/1.55 system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
}

button, textarea, input { font: inherit; color: inherit; }

.app {
  height: 100dvh;
  max-width: 820px;
  margin: 0 auto;
  display: flex;
  flex-direction: column;
  padding: 0 16px;
}

/* --- header ----------------------------------------------------------------- */

.top {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 14px 0 10px;
}

.brand { display: flex; align-items: center; gap: 12px; min-width: 0; }

.logo {
  flex: none;
  width: 38px; height: 38px;
  border-radius: 12px;
  background: linear-gradient(140deg, var(--accent), #2d9a8f);
  display: grid; place-items: center;
  color: var(--accent-ink);
}

:root[data-theme="dark"] .logo { color: #06201c; }

.brand h1 { margin: 0; font-size: 17px; line-height: 1.2; letter-spacing: -0.01em; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.brand p { margin: 0; font-size: 13px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

.top-actions { display: flex; align-items: center; gap: 10px; position: relative; }

.progress { display: flex; flex-direction: column; align-items: flex-end; gap: 4px; min-width: 96px; }
.progress[hidden] { display: none; }
.progress-label { font-size: 12px; color: var(--muted); }
.progress-bar { width: 96px; height: 5px; background: var(--line); border-radius: 999px; overflow: hidden; }
.progress-fill { height: 100%; width: 0; background: var(--accent); border-radius: 999px; transition: width 0.5s cubic-bezier(0.2, 0.7, 0.2, 1); }

.icon-btn {
  width: 38px; height: 38px;
  border: 1px solid var(--line);
  background: var(--surface);
  border-radius: 12px;
  display: grid; place-items: center;
  cursor: pointer;
  transition: background 0.15s, transform 0.1s;
}
.icon-btn:hover { background: var(--surface-2); }
.icon-btn:active { transform: scale(0.96); }

.menu {
  position: absolute; right: 0; top: 46px; z-index: 20;
  width: 232px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 14px;
  box-shadow: var(--shadow);
  padding: 6px;
}
.menu[hidden] { display: none; }
.menu button {
  width: 100%; text-align: left;
  background: none; border: 0; border-radius: 9px;
  padding: 10px 12px; cursor: pointer;
}
.menu button:hover { background: var(--surface-2); }

/* --- main / welcome ----------------------------------------------------------- */

main { flex: 1; min-height: 0; display: flex; flex-direction: column; padding-bottom: 14px; }

.welcome {
  margin: auto 0;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 24px;
  box-shadow: var(--shadow);
  padding: 34px 30px 28px;
}
.welcome h2 { margin: 0 0 10px; font-size: clamp(24px, 4.5vw, 32px); line-height: 1.15; letter-spacing: -0.02em; }
.welcome > p { margin: 0 0 18px; color: var(--muted); font-size: 17px; }

.points { list-style: none; padding: 0; margin: 0 0 22px; display: grid; gap: 10px; }
.points li { display: flex; gap: 12px; align-items: flex-start; font-size: 15px; }
.points svg { flex: none; margin-top: 3px; color: var(--accent); }

.notice {
  background: var(--accent-soft);
  border-radius: 12px;
  padding: 10px 14px;
  font-size: 14px;
  margin-bottom: 18px;
}

.field { display: grid; gap: 6px; margin-bottom: 14px; }
.field label { font-size: 14px; font-weight: 600; }
.field input {
  border: 1px solid var(--line);
  background: var(--surface-2);
  border-radius: 12px;
  padding: 11px 14px;
  width: 100%;
  max-width: 340px;
}
.field input:focus-visible, textarea:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }

.btn {
  border: 0;
  background: var(--accent);
  color: var(--accent-ink);
  border-radius: 14px;
  padding: 13px 22px;
  font-weight: 600;
  cursor: pointer;
  transition: transform 0.1s, filter 0.15s, opacity 0.15s;
}
.btn:hover { filter: brightness(1.06); }
.btn:active { transform: scale(0.98); }
.btn:disabled { opacity: 0.5; cursor: not-allowed; }
.btn.secondary { background: var(--surface); color: var(--ink); border: 1px solid var(--line); }

.error-line { color: var(--danger); font-size: 14px; margin: 8px 0 0; min-height: 1.2em; }

/* --- chat ------------------------------------------------------------------------ */

.chat { flex: 1; min-height: 0; display: flex; flex-direction: column; }
.chat[hidden], .welcome[hidden] { display: none; }

.log {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  padding: 10px 2px 18px;
  display: flex;
  flex-direction: column;
  gap: 14px;
  scroll-behavior: smooth;
}

.row { display: flex; gap: 10px; align-items: flex-end; animation: rise 0.28s ease-out both; }
.row.user { justify-content: flex-end; }

.avatar {
  flex: none; width: 30px; height: 30px; border-radius: 10px;
  background: var(--accent-soft); color: var(--accent);
  display: grid; place-items: center;
}

.bubble {
  max-width: min(88%, 600px);
  padding: 11px 15px;
  border-radius: var(--radius);
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}
.row.agent .bubble { background: var(--surface); border: 1px solid var(--line); border-bottom-left-radius: 6px; }
.row.user .bubble { background: var(--accent); color: var(--accent-ink); border-bottom-right-radius: 6px; }
.row.error .bubble { background: transparent; border: 1px dashed var(--danger); color: var(--danger); }

.typing .bubble { display: flex; gap: 5px; padding: 15px 16px; }
.typing i { width: 7px; height: 7px; border-radius: 50%; background: var(--muted); opacity: 0.5; animation: blink 1.2s infinite ease-in-out; }
.typing i:nth-child(2) { animation-delay: 0.15s; }
.typing i:nth-child(3) { animation-delay: 0.3s; }

@keyframes blink { 0%, 80%, 100% { opacity: 0.25; transform: translateY(0); } 40% { opacity: 1; transform: translateY(-3px); } }
@keyframes rise { from { opacity: 0; transform: translateY(8px); } to { opacity: 1; transform: none; } }

/* --- composer ------------------------------------------------------------------- */

.composer {
  display: flex; align-items: flex-end; gap: 8px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 20px;
  box-shadow: var(--shadow);
  padding: 8px 8px 8px 16px;
}
.composer textarea {
  flex: 1;
  border: 0; background: transparent; resize: none; outline: none;
  padding: 9px 0;
  max-height: 160px;
  line-height: 1.45;
}
.composer textarea::placeholder { color: var(--muted); }

.round {
  flex: none; width: 42px; height: 42px; border-radius: 14px; border: 0;
  display: grid; place-items: center; cursor: pointer;
  transition: transform 0.1s, filter 0.15s, background 0.15s;
}
.round:active { transform: scale(0.94); }
.round.send { background: var(--accent); color: var(--accent-ink); }
.round.send:disabled { opacity: 0.4; cursor: not-allowed; }
.round.mic { background: var(--surface-2); color: var(--ink); border: 1px solid var(--line); }
.round.mic[disabled] { opacity: 0.4; cursor: not-allowed; }
.round.mic.rec { background: var(--danger); color: #fff; border-color: transparent; animation: pulse 1.4s infinite; }
@keyframes pulse { 0% { box-shadow: 0 0 0 0 rgba(179, 38, 30, 0.45); } 100% { box-shadow: 0 0 0 12px rgba(179, 38, 30, 0); } }

.hint { font-size: 13px; color: var(--muted); min-height: 20px; padding: 6px 6px 0; }

.ended {
  display: flex; flex-wrap: wrap; gap: 10px; align-items: center; justify-content: center;
  padding: 8px 0 2px;
}

/* --- final cards ---------------------------------------------------------------- */

.card {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 20px;
  box-shadow: var(--shadow);
  padding: 20px 22px;
  animation: rise 0.35s ease-out both;
}
.card h3 { margin: 0 0 12px; font-size: 13px; letter-spacing: 0.06em; text-transform: uppercase; color: var(--muted); }
.card p { margin: 0 0 12px; white-space: pre-wrap; }
.card p:last-child { margin-bottom: 0; }

.result-head { display: flex; align-items: baseline; gap: 12px; flex-wrap: wrap; margin-bottom: 14px; }
.result-points { font-size: 40px; font-weight: 700; letter-spacing: -0.03em; line-height: 1; }
.result-points small { font-size: 16px; font-weight: 500; color: var(--muted); letter-spacing: 0; }
.chip {
  display: inline-block; padding: 4px 11px; border-radius: 999px;
  background: var(--accent-soft); color: var(--accent); font-size: 14px; font-weight: 600;
}

.meter { position: relative; margin: 6px 0 22px; }
.meter-bar { display: flex; height: 12px; border-radius: 999px; overflow: hidden; gap: 2px; }
.meter-seg { height: 100%; }
.meter-marker {
  position: absolute; top: -5px; width: 4px; height: 22px; border-radius: 3px;
  background: var(--ink); transform: translateX(-2px);
  box-shadow: 0 0 0 3px var(--surface);
  animation: slide 0.8s cubic-bezier(0.2, 0.7, 0.2, 1) both;
}
@keyframes slide { from { left: 0; opacity: 0; } }
.meter-labels { display: flex; margin-top: 8px; gap: 2px; font-size: 11px; color: var(--muted); }
.meter-labels span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; text-align: center; }
.meter-labels span.on { color: var(--ink); font-weight: 600; }

.atp { border-style: dashed; }
.atp h4 { margin: 18px 0 6px; font-size: 15px; }
.atp h4:first-of-type { margin-top: 0; }
.atp ul { margin: 0 0 6px; padding-left: 20px; }
.atp li { margin-bottom: 6px; }
.atp li .sub { display: block; color: var(--muted); font-size: 14px; }
.atp .big { font-weight: 600; }
.atp-toggle { background: none; border: 0; color: var(--accent); font-weight: 600; cursor: pointer; padding: 0; }
.atp-body[hidden] { display: none; }

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

dialog {
  width: min(620px, calc(100vw - 32px));
  border: 1px solid var(--line);
  border-radius: 20px;
  background: var(--surface);
  color: var(--ink);
  padding: 22px;
  box-shadow: var(--shadow);
}
dialog::backdrop { background: rgba(10, 12, 14, 0.45); }
dialog h3 { margin: 0 0 6px; }
dialog p { margin: 0 0 12px; color: var(--muted); font-size: 14px; }
dialog textarea {
  width: 100%; height: 220px; resize: vertical;
  border: 1px solid var(--line); border-radius: 12px; padding: 12px;
  background: var(--surface-2); font: 13px/1.45 ui-monospace, SFMono-Regular, Menlo, monospace;
}
.dialog-actions { display: flex; gap: 10px; justify-content: flex-end; margin-top: 14px; flex-wrap: wrap; }
.problems { color: var(--danger); font-size: 14px; margin: 10px 0 0; padding-left: 18px; }

@media (max-width: 560px) {
  .app { padding: 0 10px; }
  .welcome { padding: 24px 18px 20px; border-radius: 20px; }
  .bubble { max-width: 92%; }
  .brand h1 { font-size: 15px; }
  .progress { min-width: 70px; }
  .progress-bar { width: 70px; }
  .meter-labels span:not(.on) { visibility: hidden; }
}

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