:root {
  color-scheme: light dark;
  --bg: #f6f5f1;
  --surface: #ffffff;
  --surface-2: #efede7;
  --text: #1f1e1c;
  --muted: #6b6a65;
  --border: rgba(0, 0, 0, 0.12);
  --accent: #0f6e56;
  --accent-text: #ffffff;
  --mine: #e1f5ee;
  --mine-text: #085041;
  --danger: #a32d2d;
  --link: #185fa5;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #161614;
    --surface: #23221f;
    --surface-2: #2c2b28;
    --text: #ecebe6;
    --muted: #a3a29b;
    --border: rgba(255, 255, 255, 0.14);
    --accent: #1d9e75;
    --accent-text: #04342c;
    --mine: #085041;
    --mine-text: #e1f5ee;
    --danger: #f09595;
    --link: #85b7eb;
  }
}

* { box-sizing: border-box; }

html, body { margin: 0; min-height: 100%; background: var(--bg); color: var(--text); }

body { display: flex; flex-direction: column; min-height: 100dvh; font-size: 16px; line-height: 1.45; }

[hidden] { display: none !important; }

h1, h2 { font-weight: 600; margin: 0; }
h1 { font-size: 18px; }
h2 { font-size: 20px; margin-bottom: 8px; }
p { margin: 0; }

.muted { color: var(--muted); font-size: 14px; }
.error { color: var(--danger); font-size: 14px; min-height: 0; }
.error:empty { display: none; }

.bar {
  position: sticky; top: 0; z-index: 2;
  display: flex; align-items: center; gap: 12px;
  padding: calc(env(safe-area-inset-top) + 10px) 16px 10px;
  background: var(--surface); border-bottom: 1px solid var(--border);
}
.bar-text { flex: 1; min-width: 0; }
.bar-text .muted { font-size: 13px; }
.logo { width: 28px; height: 28px; color: var(--accent); flex: none; }

.icon-button { width: 40px; height: 40px; padding: 8px; border: none; background: none; color: var(--muted); border-radius: 50%; }
.icon-button svg { width: 24px; height: 24px; }

button {
  font: inherit; font-size: 15px; padding: 10px 16px; border-radius: 10px;
  border: 1px solid var(--border); background: var(--surface); color: var(--text); cursor: pointer;
}
button.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); font-weight: 600; }
button:disabled { opacity: 0.6; }

.screen { flex: 1; width: 100%; max-width: 640px; margin: 0 auto; padding: 16px; }

#pair, #problem { display: flex; flex-direction: column; justify-content: center; gap: 12px; text-align: center; }
.pair-mark { width: 64px; height: 64px; margin: 0 auto 8px; color: var(--accent); }
#pair-form { display: flex; flex-direction: column; gap: 10px; text-align: left; margin-top: 16px; }
#pair-form label { font-size: 14px; color: var(--muted); }
input {
  font: inherit; font-size: 17px; letter-spacing: 0.05em; padding: 12px; border-radius: 10px;
  border: 1px solid var(--border); background: var(--surface); color: var(--text); width: 100%;
}

.setup { background: var(--surface); border: 1px solid var(--border); border-radius: 14px; padding: 14px; margin-bottom: 16px; display: flex; flex-direction: column; gap: 10px; }
.setup-buttons { display: flex; gap: 10px; flex-wrap: wrap; justify-content: flex-end; }

#messages { list-style: none; margin: 0; padding: 0 0 calc(env(safe-area-inset-bottom) + 16px); display: flex; flex-direction: column; gap: 10px; }

.day { align-self: center; font-size: 12px; color: var(--muted); padding: 8px 0 2px; }

.msg { max-width: 85%; border-radius: 16px; padding: 10px 12px; overflow-wrap: anywhere; }
.msg .time { font-size: 11px; color: var(--muted); margin-top: 4px; }
.msg.me { align-self: flex-end; background: var(--mine); color: var(--mine-text); border-bottom-right-radius: 4px; }
.msg.me .time { color: inherit; opacity: 0.75; text-align: right; }
.msg.me .said { display: flex; gap: 6px; }
.msg.me .said svg { width: 16px; height: 16px; flex: none; margin-top: 3px; }
.msg.ai { align-self: flex-start; background: var(--surface); border: 1px solid var(--border); border-bottom-left-radius: 4px; }
.msg .label { font-weight: 600; font-size: 14px; }
.msg .meta { font-size: 13px; color: var(--muted); }
.msg .body { white-space: pre-wrap; font-size: 15px; margin-top: 2px; }
.msg a { display: inline-block; margin-top: 6px; color: var(--link); font-size: 14px; text-decoration: none; }
.msg.system { align-self: center; max-width: 92%; background: var(--surface-2); border: none; text-align: center; font-size: 14px; }
.msg.result-failed .label, .msg.result-refused .label { color: var(--danger); }

.empty { text-align: center; color: var(--muted); padding: 48px 16px; }

dialog { border: 1px solid var(--border); border-radius: 16px; background: var(--surface); color: var(--text); padding: 20px; width: min(92vw, 420px); }
dialog::backdrop { background: rgba(0, 0, 0, 0.45); }
dialog form { display: flex; flex-direction: column; gap: 12px; }
