/* dad-chat: chat with Grisha for an older reader.
   Paper and malachite: the warm page of a field notebook, the green of the
   mineral. Large type, high contrast, one obvious action per state. */

:root {
  --bg: #F2EADD;              /* must equal theme-color and manifest colours byte for byte */
  --paper: #FFFDF8;
  --ink: #1D1B18;
  --muted: #4F493F;
  --line: #D6CBB9;
  --accent: #1A6953;
  --accent-ink: #FFFFFF;
  --accent-soft: #DCE9E2;
  --danger: #8C2F1C;
  --ochre: #C9A46A;
  --fs: 16px;
  --fs-small: 14px;
  --radius: 18px;
  --tap: 48px;
  --dock-pad-b: max(calc(env(safe-area-inset-bottom) - 10px), 10px);
  --font-body: -apple-system, BlinkMacSystemFont, "Segoe UI", system-ui, "Helvetica Neue", Arial, sans-serif;
  --font-display: "Iowan Old Style", "Palatino Linotype", Palatino, "Book Antiqua", Georgia, serif;
  --ease: cubic-bezier(.2, .8, .2, 1);
}
html.text-xl { --fs: 19px; --fs-small: 16px; }

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html { height: 100%; overscroll-behavior-y: contain; background: var(--bg); -webkit-text-size-adjust: 100%; }
body {
  height: 100%; margin: 0; overflow: hidden; overscroll-behavior-y: none;
  background: var(--bg); color: var(--ink);
  font: var(--fs)/1.45 var(--font-body);
  text-rendering: optimizeLegibility;
}
button, input, textarea { font: inherit; color: inherit; }
button { touch-action: manipulation; cursor: pointer; }
[hidden] { display: none !important; }
.display { font-family: var(--font-display); font-weight: 600; letter-spacing: -0.01em; }

#shell {
  position: fixed; top: 0; left: 0; right: 0; height: 100%;
  display: flex; flex-direction: column;
  max-width: 640px; margin: 0 auto;
  will-change: transform;
}
.screen { flex: 1 1 auto; min-height: 0; display: flex; flex-direction: column; }
.screen-scroll {
  flex: 1 1 auto; min-height: 0; overflow-y: auto; -webkit-overflow-scrolling: touch;
  overscroll-behavior-y: contain;
  padding: calc(env(safe-area-inset-top) + 28px) 22px calc(env(safe-area-inset-bottom) + 28px);
}
.screen-scroll.center { display: flex; flex-direction: column; justify-content: center; text-align: center; }
.hero-icon { width: 88px; height: 88px; display: block; margin: 0 0 12px -10px; }
.center .hero-icon { margin-left: auto; margin-right: auto; }
h1.display { font-size: 30px; line-height: 1.15; margin: 0 0 14px; text-wrap: balance; }
.lead { margin: 0 0 14px; color: var(--ink); text-wrap: pretty; }

/* ── Forms ── */
#form-register { margin-top: 22px; }
.field { display: block; margin: 0 0 18px; }
.field span { display: block; font-size: var(--fs-small); font-weight: 600; color: var(--muted); margin: 0 0 6px; }
.field input {
  width: 100%; min-height: 50px; padding: 10px 14px;
  border: 2px solid var(--line); border-radius: 14px; background: var(--paper);
  font-size: 17px; outline: none;
}
.field input:focus { border-color: var(--accent); }
.field input[aria-invalid="true"] { border-color: var(--danger); }
.field-error, .form-error { display: block; font-style: normal; color: var(--danger); font-size: var(--fs-small); margin-top: 6px; min-height: 0; }
.form-error:empty, .field-error:empty { display: none; }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 10px;
  min-height: var(--tap); padding: 10px 22px; border-radius: 14px; border: 0;
  font-weight: 600; font-size: var(--fs); text-decoration: none; line-height: 1.2;
}
.btn-block { width: 100%; }
.btn-primary { background: var(--accent); color: var(--accent-ink); }
.btn-primary:disabled { opacity: .55; }
.btn-secondary { background: var(--paper); color: var(--accent); border: 2px solid var(--accent); }
.btn-ghost { background: transparent; color: var(--accent); }
.btn + .btn { margin-top: 12px; }
#screen-wait .btn { margin-top: 18px; }

/* ── Top bar ── */
.topbar {
  flex: 0 0 auto; display: flex; align-items: center; gap: 12px;
  padding: calc(env(safe-area-inset-top) + 8px) 10px 10px 16px;
  background: var(--bg); border-bottom: 1px solid var(--line);
  position: relative; z-index: 30;
}
.topbar-icon { width: 40px; height: 40px; border-radius: 10px; }
.topbar-title { flex: 1; min-width: 0; display: flex; flex-direction: column; line-height: 1.15; }
.topbar-title strong { font-size: 22px; }
#topbar-status { font-size: 13px; color: var(--muted); }
#topbar-status.typing { color: var(--accent); font-weight: 600; }
.icon-btn {
  width: var(--tap); height: var(--tap); flex: 0 0 auto; border: 0; background: transparent;
  display: inline-flex; align-items: center; justify-content: center; border-radius: 50%; color: var(--ink);
}
.icon-btn svg { fill: currentColor; }
.banner { flex: 0 0 auto; background: #F7E3C4; color: #5B3A08; text-align: center; padding: 8px 14px; font-size: var(--fs-small); }

/* ── Messages ── */
#main {
  flex: 1 1 auto; min-height: 0; overflow-y: auto; -webkit-overflow-scrolling: touch;
  overscroll-behavior-y: contain; padding: 14px 12px 18px;
}
.load-older { display: flex; margin: 0 auto 10px; }
.day { text-align: center; margin: 18px 0 10px; }
.day span { display: inline-block; font-size: 13px; font-weight: 600; color: var(--muted); background: #E8DECD; padding: 3px 12px; border-radius: 999px; }
.msg { display: flex; flex-direction: column; margin: 0 0 10px; }
.msg.user { align-items: flex-end; }
.msg.assistant { align-items: flex-start; }
.bubble {
  max-width: 88%; padding: 12px 16px 8px; border-radius: var(--radius);
  overflow-wrap: anywhere; word-break: normal; position: relative;
}
.msg.user .bubble { background: var(--accent); color: var(--accent-ink); border-bottom-right-radius: 6px; }
.msg.assistant .bubble { background: var(--paper); border: 1px solid var(--line); border-bottom-left-radius: 6px; max-width: 94%; }
.bubble .time { display: block; text-align: right; font-size: 12px; opacity: .75; margin-top: 4px; }
.msg.user .bubble .time { color: #E6F1EC; opacity: 1; }
.bubble.error { border-color: var(--danger); }
.bubble .err-note { color: var(--danger); }
.msg-actions { margin-top: 6px; display: flex; gap: 8px; flex-wrap: wrap; }
.msg-actions .btn { min-height: 42px; font-size: var(--fs-small); padding: 8px 16px; }

/* Markdown inside Grisha's bubble */
.md > :first-child { margin-top: 0; }
.md > :last-child { margin-bottom: 0; }
.md p { margin: 0 0 10px; }
.md h1, .md h2, .md h3, .md h4 { font-family: var(--font-display); font-weight: 600; line-height: 1.2; margin: 14px 0 8px; }
.md h1 { font-size: 1.3em; } .md h2 { font-size: 1.2em; } .md h3, .md h4 { font-size: 1.08em; }
.md ul, .md ol { margin: 0 0 10px; padding-left: 1.3em; }
.md li { margin: 3px 0; }
.md a { color: var(--accent); font-weight: 600; text-decoration-thickness: 2px; text-underline-offset: 3px; }
.md code { font-family: ui-monospace, Menlo, monospace; font-size: .86em; background: #EFE7DA; padding: 1px 5px; border-radius: 5px; }
.md pre { background: #EFE7DA; padding: 10px 12px; border-radius: 10px; overflow-x: auto; font-size: 13px; }
.md pre code { background: none; padding: 0; }
.md blockquote { margin: 0 0 10px; padding: 2px 0 2px 12px; border-left: 1px solid var(--line); color: var(--muted); font-style: italic; }
.md hr { border: 0; border-top: 1px solid var(--line); margin: 12px 0; }
.md .table-wrap { overflow-x: auto; margin: 0 0 10px; -webkit-overflow-scrolling: touch; }
.md table { border-collapse: collapse; font-size: var(--fs-small); overflow-wrap: normal; word-break: normal; }
.md th, .md td { border: 1px solid var(--line); padding: 6px 9px; text-align: left; vertical-align: top; hyphens: manual; }
.md th { white-space: nowrap; }
.md th { background: #F1E9DC; font-weight: 700; }
.md img { max-width: 100%; border-radius: 10px; }

.typing-line { display: flex; align-items: center; gap: 10px; color: var(--muted); font-size: var(--fs-small); padding: 2px 0 6px; }
.dots { display: inline-flex; gap: 5px; }
.dots i { width: 8px; height: 8px; border-radius: 50%; background: var(--accent); opacity: .35; animation: dot 1.2s infinite var(--ease); }
.dots i:nth-child(2) { animation-delay: .15s; } .dots i:nth-child(3) { animation-delay: .3s; }
@keyframes dot { 0%, 80%, 100% { opacity: .25; transform: translateY(0); } 40% { opacity: 1; transform: translateY(-3px); } }
.cursor::after { content: ""; display: inline-block; width: .5em; height: 1em; background: var(--accent); margin-left: 2px; vertical-align: -0.12em; animation: blink 1s steps(2) infinite; }
@keyframes blink { 50% { opacity: 0; } }

/* Attachments */
.att-img { display: block; max-width: 100%; max-height: 360px; border-radius: 12px; margin: 0 0 8px; background: #E8DECD; }
.att-video { display: block; width: 100%; max-width: 100%; max-height: 420px; border-radius: 12px; margin: 0 0 8px; background: #1d1a16; }
.bubble > * + .att-img, .bubble > * + .att-video { margin-top: 14px; }
.file-btn {
  display: flex; align-items: center; gap: 12px; width: 100%; min-height: 52px; margin: 6px 0;
  padding: 8px 14px; border-radius: 14px; text-decoration: none; font-weight: 600;
  background: var(--accent-soft); color: var(--ink); border: 1px solid #B7D1C4;
}
.msg.user .file-btn { background: rgba(255,255,255,.14); color: #fff; border-color: rgba(255,255,255,.35); }
.file-btn svg { flex: 0 0 auto; }
.file-btn span { display: flex; flex-direction: column; min-width: 0; line-height: 1.2; }
.file-btn b { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-weight: 600; }
.file-btn small { font-weight: 400; font-size: 13px; opacity: .85; }
.link-btns { display: flex; flex-direction: column; gap: 8px; margin: 8px 0 4px; }
.link-btn { display: flex; align-items: center; justify-content: center; min-height: 48px; padding: 8px 16px; border-radius: 14px; background: var(--accent); color: #fff; text-decoration: none; font-weight: 600; text-align: center; }
.voice-label { display: flex; align-items: center; gap: 8px; font-size: 13px; opacity: .9; margin-bottom: 4px; }

.empty { text-align: center; color: var(--muted); padding: 48px 16px; }
.empty-title { font-size: 26px; color: var(--ink); margin: 0 0 10px; }
.invite { background: var(--paper); border: 1px solid var(--line); border-radius: var(--radius); padding: 14px 16px; margin: 6px 0 14px; }
.invite p { margin: 0 0 10px; }

/* ── Dock / composer ── */
.dock {
  flex: 0 0 auto; position: relative; z-index: 35;
  background: var(--bg); border-top: 1px solid var(--line);
  padding: 8px 8px var(--dock-pad-b);
}
.composer { display: flex; align-items: flex-end; gap: 6px; }
#input {
  flex: 1; min-width: 0; min-height: var(--tap); max-height: 168px; resize: none;
  padding: 10px 14px; border-radius: 24px; border: 2px solid var(--line); background: var(--paper);
  font-size: var(--fs); line-height: 1.35; outline: none;
}
#input:focus { border-color: var(--accent); }
.composer-attach { color: var(--muted); }
.round-btn {
  width: 50px; height: 50px; flex: 0 0 auto; border-radius: 50%; border: 0;
  background: var(--accent); color: #fff; display: inline-flex; align-items: center; justify-content: center;
}
.round-btn:disabled { opacity: .5; }
.recorder { display: flex; align-items: center; justify-content: space-between; gap: 10px; min-height: 50px; }
.rec-status { display: flex; align-items: center; gap: 10px; font-size: 20px; font-variant-numeric: tabular-nums; font-weight: 600; }
.rec-dot { width: 14px; height: 14px; border-radius: 50%; background: #C0391B; animation: pulse 1.2s infinite var(--ease); }
@keyframes pulse { 50% { opacity: .3; } }
.attach-preview { display: flex; align-items: center; gap: 12px; padding: 4px 4px 10px; }
.attach-thumb { width: 56px; height: 56px; border-radius: 10px; background: var(--accent-soft) center/cover no-repeat; flex: 0 0 auto; display: flex; align-items: center; justify-content: center; color: var(--accent); }
.attach-meta { flex: 1; min-width: 0; display: flex; flex-direction: column; line-height: 1.2; }
.attach-meta strong { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: var(--fs-small); }
.attach-meta span { font-size: 13px; color: var(--muted); }
.upload-progress { height: 6px; background: var(--line); border-radius: 3px; margin: 0 4px 8px; overflow: hidden; }
#upload-bar { height: 100%; width: 0; background: var(--accent); transition: width .2s linear; }

/* ── Sheet, scrim, toast (outside #shell) ── */
.scrim { position: fixed; inset: -15vh 0; background: rgba(29, 27, 24, .38); z-index: 1900; }
.sheet {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 2000; max-width: 640px; margin: 0 auto;
  background: var(--bg); border-radius: 22px 22px 0 0; padding: 10px 20px calc(env(safe-area-inset-bottom) + 18px);
  box-shadow: 0 -8px 30px rgba(0, 0, 0, .12);
  animation: sheet-in .28s var(--ease);
}
@keyframes sheet-in { from { transform: translateY(100%); } to { transform: none; } }
.sheet-handle { width: 44px; height: 5px; border-radius: 3px; background: var(--line); margin: 0 auto 14px; }
.sheet { display: flex; flex-direction: column; gap: 12px; }
.sheet .btn + .btn { margin-top: 0; }
.sheet h2 { font-size: 22px; margin: 0 0 4px; }
.sheet-note { font-size: var(--fs-small); color: var(--muted); margin: 0 2px; }
.sheet-note:empty { display: none; }
.toast {
  position: fixed; left: 50%; transform: translateX(-50%); bottom: calc(env(safe-area-inset-bottom) + 96px);
  z-index: 2100; max-width: calc(100% - 32px); background: var(--ink); color: #fff; padding: 12px 18px; border-radius: 14px;
  font-size: var(--fs-small); text-align: center;
}

/* ── Pull to refresh ── */
.ptr { position: fixed; top: 0; left: 0; right: 0; height: 0; display: flex; justify-content: center; z-index: 90; pointer-events: none; opacity: 0; will-change: transform, opacity; }
.ptr-ring { display: block; padding: 0; margin-top: 6px; width: 26px; height: 26px; border-radius: 50%; border: 3px solid var(--line); border-top-color: var(--accent); box-sizing: border-box; }
.ptr.spinning .ptr-ring { animation: ptrspin .7s linear infinite; }
@keyframes ptrspin { to { transform: rotate(360deg); } }
.sa-probe { position: fixed; top: 0; left: 0; width: 1px; height: env(safe-area-inset-top); visibility: hidden; pointer-events: none; }

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

.info-btn svg { fill: none; stroke: currentColor; }
.screen-scroll { position: relative; }
.info-float { position: absolute; top: calc(env(safe-area-inset-top) + 22px); right: 12px; color: var(--muted); }
