:root {
  --bg: #faf8f5;
  --surface: #ffffff;
  --ink: #1a1918;
  --muted: #6b6661;
  --line: #e4dfd8;
  --accent: #d93a1f;
  --accent-ink: #ffffff;
  --focus: #d93a1f66;
  --shadow: 0 1px 2px #0000000d, 0 4px 16px #0000000a;
  --radius: 16px;
  --gutter: 16px;
  color-scheme: light dark;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #141312;
    --surface: #1f1d1b;
    --ink: #f2efe9;
    --muted: #a39d95;
    --line: #34312d;
    --accent: #ff6a4d;
    --accent-ink: #1a0905;
    --focus: #ff6a4d66;
    --shadow: none;
  }
}

*, *::before, *::after { box-sizing: border-box; }

html { -webkit-text-size-adjust: 100%; }

body {
  margin: 0 auto;
  max-width: 960px;
  min-height: 100dvh;
  padding:
    max(var(--gutter), env(safe-area-inset-top))
    max(var(--gutter), env(safe-area-inset-right))
    max(var(--gutter), env(safe-area-inset-bottom))
    max(var(--gutter), env(safe-area-inset-left));
  background: var(--bg);
  color: var(--ink);
  font: 16px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  display: flex;
  flex-direction: column;
  gap: 20px;
}

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

/* Masthead */
.masthead { text-align: center; padding-top: 8px; }
.masthead h1 {
  margin: 0;
  font-size: clamp(2.5rem, 12vw, 4rem);
  font-weight: 900;
  letter-spacing: 0.04em;
  line-height: 1;
  color: var(--accent);
}
.tagline { margin: 6px 0 0; color: var(--muted); }

/* Panels */
.panels { display: grid; gap: 8px; }

.panel {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 12px;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.panel-label {
  font-size: 0.8125rem;
  font-weight: 700;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--muted);
  padding: 0 4px;
}

textarea {
  width: 100%;
  min-height: 7.5rem;
  resize: vertical;
  border: 0;
  border-radius: 10px;
  padding: 8px 4px;
  background: transparent;
  color: var(--ink);
  font: inherit;
  font-size: 1.125rem; /* >=16px keeps iOS from zooming on focus */
  overflow-wrap: anywhere;
}
textarea::placeholder { color: var(--muted); opacity: 0.7; }
textarea:focus { outline: none; }
.panel:focus-within { border-color: var(--accent); box-shadow: 0 0 0 3px var(--focus); }

/* Room above and below each line for stacked diacritics. */
.panel-scream textarea {
  font-size: 1.625rem;
  line-height: 2.1;
  letter-spacing: 0.06em;
  padding-top: 4px;
}

.link-mark {
  text-align: center;
  color: var(--muted);
  font-size: 1.25rem;
  line-height: 1;
  user-select: none;
}

/* Action buttons: full-width row, big thumb targets. */
.actions { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }

button {
  appearance: none;
  min-height: 44px;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: var(--bg);
  color: var(--ink);
  font: inherit;
  font-size: 0.9375rem;
  font-weight: 600;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  cursor: pointer;
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
  transition: transform 80ms ease, background-color 120ms ease;
}
button:hover { border-color: var(--muted); }
button:active { transform: scale(0.97); }
button:focus-visible { outline: 3px solid var(--focus); outline-offset: 1px; }
button svg {
  width: 18px;
  height: 18px;
  flex: none;
  fill: none;
  stroke: currentColor;
  stroke-width: 2;
  stroke-linecap: round;
  stroke-linejoin: round;
}
button[data-action="share"] {
  background: var(--accent);
  border-color: var(--accent);
  color: var(--accent-ink);
}

/* Cipher key */
.key {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 0 12px;
}
.key summary {
  cursor: pointer;
  font-weight: 700;
  padding: 12px 4px;
  min-height: 44px;
  list-style-position: inside;
}
.key-grid {
  list-style: none;
  margin: 0;
  padding: 4px 0 12px;
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(64px, 1fr));
  gap: 6px;
}
.key-grid li {
  display: flex;
  align-items: baseline;
  justify-content: center;
  gap: 8px;
  padding: 10px 0;
  border-radius: 10px;
  background: var(--bg);
}
.key-grid .letter { color: var(--muted); font-size: 0.875rem; font-weight: 600; min-width: 1ch; }
.key-grid .glyph { font-size: 1.5rem; line-height: 1.6; }

.credits { margin-top: auto; text-align: center; color: var(--muted); font-size: 0.8125rem; }

/* Toast */
.toast {
  position: fixed;
  left: 50%;
  bottom: max(24px, env(safe-area-inset-bottom));
  transform: translate(-50%, 16px);
  max-width: calc(100vw - 2 * var(--gutter));
  padding: 10px 16px;
  border-radius: 999px;
  background: var(--ink);
  color: var(--bg);
  font-size: 0.9375rem;
  font-weight: 600;
  text-align: center;
  opacity: 0;
  pointer-events: none;
  transition: opacity 160ms ease, transform 160ms ease;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }

/* Side-by-side on wider screens. */
@media (min-width: 760px) {
  .panels { grid-template-columns: 1fr auto 1fr; align-items: stretch; }
  .panel textarea { flex: 1; min-height: 14rem; }
  .link-mark { align-self: center; transform: rotate(90deg); }
}

@media (prefers-reduced-motion: reduce) {
  button, .toast { transition: none; }
}
