/* ---------- theme ---------- */
:root {
  color-scheme: dark light;
  --bg: #171412;
  --surface: #262019;
  --surface-2: #2f2820;
  --border: #3a3128;
  --text: #ede6dd;
  --muted: #a2968a;
  --accent: #d97a4a;
  --accent-soft: #d97a4a2b;
  --on-accent: #fff8f2;
  --error: #e8846f;
  --code-bg: #00000040;
}

@media (prefers-color-scheme: light) {
  :root {
    --bg: #faf6f0;
    --surface: #ffffff;
    --surface-2: #f1eae1;
    --border: #e5dcd0;
    --text: #33291f;
    --muted: #8a7d6e;
    --accent: #c25e33;
    --accent-soft: #c25e331f;
    --on-accent: #fff8f2;
    --error: #b3402a;
    --code-bg: #00000010;
  }
}

/* ---------- base ---------- */
* { box-sizing: border-box; }

html, body { height: 100%; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto,
               "Helvetica Neue", Arial, sans-serif;
  font-size: 16px;
  line-height: 1.45;
  -webkit-font-smoothing: antialiased;
  overscroll-behavior-y: none;
}

.screen {
  height: 100dvh;
  display: flex;
  flex-direction: column;
}

.hidden { display: none !important; }

button {
  font: inherit;
  color: inherit;
  border: 0;
  background: none;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
}

/* ---------- login ---------- */
#login {
  align-items: center;
  justify-content: center;
  padding: 24px;
  padding-top: calc(24px + env(safe-area-inset-top));
  padding-bottom: calc(24px + env(safe-area-inset-bottom));
}

#login-form {
  width: 100%;
  max-width: 320px;
  display: flex;
  flex-direction: column;
  align-items: stretch;
  text-align: center;
  gap: 12px;
}

.login-mark { font-size: 56px; line-height: 1; }

#login-form h1 {
  margin: 4px 0 0;
  font-size: 28px;
  letter-spacing: -0.02em;
}

.login-sub {
  margin: 0 0 16px;
  color: var(--muted);
  font-size: 14.5px;
}

#password {
  font: inherit;
  font-size: 16px; /* prevents iOS zoom */
  color: var(--text);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 13px 16px;
  min-height: 48px;
  outline: none;
  text-align: center;
}

#password:focus { border-color: var(--accent); }

#login-btn {
  min-height: 48px;
  border-radius: 14px;
  background: var(--accent);
  color: var(--on-accent);
  font-weight: 600;
  font-size: 16px;
}

#login-btn:disabled { opacity: 0.6; }

.login-error {
  margin: 4px 0 0;
  color: var(--error);
  font-size: 14px;
}

/* ---------- top bar ---------- */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 6px 12px 6px 16px;
  padding-top: calc(6px + env(safe-area-inset-top));
  padding-left: calc(16px + env(safe-area-inset-left));
  padding-right: calc(12px + env(safe-area-inset-right));
  border-bottom: 1px solid var(--border);
  background: var(--bg);
  flex: none;
}

.topbar-brand {
  display: flex;
  align-items: center;
  gap: 8px;
  min-height: 44px;
}

.topbar-mark { font-size: 18px; }

.topbar-title {
  font-size: 17px;
  font-weight: 650;
  letter-spacing: -0.01em;
}

.sync-btn {
  display: inline-flex;
  align-items: center;
  gap: 7px;
  min-height: 44px;
  padding: 0 17px;
  border-radius: 22px;
  background: var(--accent);
  color: var(--on-accent);
  font-size: 14.5px;
  font-weight: 600;
}

.sync-btn:active { filter: brightness(0.92); }

.sync-btn:disabled { opacity: 0.55; }

.sync-btn .sync-icon {
  display: inline-block;
  font-size: 17px;
  line-height: 1;
}

.sync-btn.busy .sync-icon {
  animation: spin 1s linear infinite;
}

@keyframes spin {
  to { transform: rotate(360deg); }
}

/* ---------- thread ---------- */
#thread {
  flex: 1;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  display: flex;
  flex-direction: column;
  gap: 10px;
  padding: 16px 14px 20px;
  padding-left: calc(14px + env(safe-area-inset-left));
  padding-right: calc(14px + env(safe-area-inset-right));
}

.msg {
  max-width: 86%;
  padding: 10px 14px;
  border-radius: 18px;
  overflow-wrap: break-word;
}

.msg.user {
  align-self: flex-end;
  background: var(--accent);
  color: var(--on-accent);
  border-bottom-right-radius: 6px;
  white-space: pre-wrap;
}

/* friendly label bubble for the header Sync action */
.msg.user.sync { font-weight: 600; }
.msg.user.sync::before { content: '⟳ '; }

.msg.coach {
  align-self: flex-start;
  background: var(--surface);
  border: 1px solid var(--border);
  border-bottom-left-radius: 6px;
}

.msg.coach p { margin: 0 0 0.6em; }
.msg.coach p:last-child,
.msg.coach ul:last-child { margin-bottom: 0; }

.msg.coach p.h { font-weight: 700; }

.msg.coach ul {
  margin: 0 0 0.6em;
  padding-left: 20px;
}

.msg.coach li { margin: 0.15em 0; }

.msg.coach code {
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 0.88em;
  background: var(--code-bg);
  border-radius: 5px;
  padding: 1px 5px;
}

.msg.coach a {
  color: var(--accent);
  text-decoration: underline;
  text-underline-offset: 2px;
}

/* tool activity chips */
.tool {
  align-self: flex-start;
  display: inline-flex;
  align-items: baseline;
  gap: 6px;
  max-width: 90%;
  padding: 4px 11px;
  border: 1px solid var(--border);
  border-radius: 999px;
  background: transparent;
  color: var(--muted);
  font-size: 12px;
}

.tool .tool-name { font-weight: 650; flex: none; }

.tool .tool-summary {
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 11px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

/* notices + run meta */
.notice {
  align-self: center;
  max-width: 92%;
  text-align: center;
  color: var(--muted);
  font-size: 12.5px;
  padding: 2px 8px;
}

.notice.error { color: var(--error); }

/* inline action inside a notice (e.g. "stop it" on the busy notice) */
.notice-stop {
  background: none;
  border: none;
  padding: 0;
  font: inherit;
  color: inherit;
  text-decoration: underline;
  cursor: pointer;
}

.notice-stop:disabled {
  opacity: 0.5;
  cursor: default;
}

.meta {
  align-self: flex-start;
  color: var(--muted);
  font-size: 11px;
  margin-top: -4px;
  padding-left: 8px;
}

/* "new conversation" separator (history restore) */
.divider {
  align-self: stretch;
  display: flex;
  align-items: center;
  gap: 10px;
  margin: 8px 0;
  color: var(--muted);
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.06em;
}

.divider::before,
.divider::after {
  content: '';
  flex: 1;
  height: 1px;
  background: var(--border);
}

.notice.interrupted { color: var(--muted); }

/* ---------- dock (working + quick actions + composer) ---------- */
.dock {
  flex: none;
  background: var(--bg);
  border-top: 1px solid var(--border);
  padding: 8px 10px calc(10px + env(safe-area-inset-bottom));
  padding-left: calc(10px + env(safe-area-inset-left));
  padding-right: calc(10px + env(safe-area-inset-right));
  position: relative;
}

.working {
  position: absolute;
  top: -1px;
  left: 0;
  right: 0;
  pointer-events: none;
}

.working-line {
  height: 2px;
  background: linear-gradient(90deg,
    transparent 0%, var(--accent) 35%, var(--accent) 65%, transparent 100%);
  background-size: 200% 100%;
  animation: slide 1.2s linear infinite;
}

@keyframes slide {
  from { background-position: 200% 0; }
  to   { background-position: -200% 0; }
}

.working-label {
  position: absolute;
  top: -8px;
  left: 50%;
  transform: translateX(-50%) translateY(-100%);
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 3px 12px;
  color: var(--muted);
  font-size: 11.5px;
}

/* quick actions */
.quick {
  display: flex;
  gap: 8px;
  overflow-x: auto;
  padding-bottom: 8px;
  scrollbar-width: none;
}

.quick::-webkit-scrollbar { display: none; }

.chip {
  flex: none;
  min-height: 44px;
  padding: 0 15px;
  border: 1px solid var(--border);
  border-radius: 22px;
  background: var(--surface);
  font-size: 14px;
  white-space: nowrap;
}

.chip:active { background: var(--surface-2); }

.chip:disabled {
  opacity: 0.45;
  pointer-events: none;
}

.chip.armed {
  border-color: var(--accent);
  background: var(--accent-soft);
}

/* composer */
.composer {
  display: flex;
  align-items: flex-end;
  gap: 8px;
}

#input {
  flex: 1;
  font: inherit;
  font-size: 16px; /* prevents iOS zoom */
  color: var(--text);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 22px;
  padding: 11px 16px;
  min-height: 44px;
  max-height: 160px;
  resize: none;
  outline: none;
  line-height: 1.4;
}

#input:focus { border-color: var(--accent); }

#input::placeholder { color: var(--muted); }

#send {
  flex: none;
  width: 44px;
  height: 44px;
  border-radius: 50%;
  background: var(--accent);
  color: var(--on-accent);
  font-size: 20px;
  font-weight: 700;
  display: flex;
  align-items: center;
  justify-content: center;
}

#send:disabled { opacity: 0.45; }

/* stop mode: same spot, square glyph, stays tappable while streaming */
#send.stop { font-size: 15px; }
