/*
 * Morse: shared structure for the one-screen app.
 * Everything a phone needs sits inside one fixed shell with no page scroll.
 * Colors, type and shape come from tokens that each look (skins/*.css) sets.
 */

:root {
  --ground: #ECEAE4;
  --surface: #F7F6F2;
  --surface-2: #FFFFFF;
  --ink: #18181A;
  --muted: #5E5B55;
  --line: #D3D0C8;
  --accent: #C2410C;
  --on-accent: #FFFFFF;
  --deck: #1C1D1F;
  --deck-ink: #F2F1EC;
  --deck-muted: #A8A69F;
  --deck-line: #34363A;
  --key: #2C2D30;
  --key-ink: #F7F6F2;
  --key-ring: #F97316;
  --mark: #8A8780;
  --mark-hot: #F97316;
  --radius: 22px;
  --radius-sm: 14px;
  --font-ui: "Inter", system-ui, -apple-system, "Segoe UI", sans-serif;
  --font-display: "Inter", system-ui, sans-serif;
  --font-mono: "JetBrains Mono", ui-monospace, "SF Mono", Menlo, monospace;
  --skin-ground: var(--ground);
  --gap: 8px;
}

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

html { height: 100%; overflow-x: hidden; -webkit-text-size-adjust: 100%; }

body {
  height: 100%;
  margin: 0;
  background: var(--ground);
  color: var(--ink);
  font-family: var(--font-ui);
  font-size: 16px;
  line-height: 1.35;
  overscroll-behavior: none;
  -webkit-tap-highlight-color: transparent;
}

button, input, textarea, select { font: inherit; color: inherit; }
button { cursor: pointer; border: 0; background: none; padding: 0; }
button:disabled, [aria-disabled="true"] { opacity: .45; cursor: default; }
a { color: inherit; text-decoration: none; }
[hidden] { display: none !important; }
h1, h2, p, ol { margin: 0; }

:focus-visible { outline: 3px solid var(--accent); outline-offset: 2px; }

.sr {
  position: absolute; width: 1px; height: 1px; overflow: hidden;
  clip-path: inset(50%); white-space: nowrap;
}

/* ---------- the shell ---------- */

.app {
  position: fixed;
  inset: 0;
  width: 100%;
  max-width: 480px;
  margin: 0 auto;
  display: flex;
  flex-direction: column;
  gap: var(--gap);
  padding:
    max(8px, env(safe-area-inset-top))
    max(12px, env(safe-area-inset-right))
    max(10px, env(safe-area-inset-bottom))
    max(12px, env(safe-area-inset-left));
  overflow-x: hidden;
  overflow-y: auto; /* only very short screens ever scroll, and only inside the shell */
}

html[data-boot-mode="decode"] .app { visibility: hidden; }

/* ---------- top bar ---------- */

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

.brand { display: flex; align-items: center; gap: 7px; font-family: var(--font-display); font-size: 19px; font-weight: 700; letter-spacing: -.02em; margin-right: auto; }
.brand-mark { display: inline-flex; align-items: center; gap: 3px; }
.brand-mark i { width: 6px; height: 6px; border-radius: 50%; background: var(--accent); }
.brand-mark b { width: 15px; height: 6px; border-radius: 3px; background: var(--ink); }

.modes { display: flex; padding: 2px; border-radius: 999px; background: var(--surface); border: 1px solid var(--line); }
.mode {
  min-height: 44px; padding: 0 16px; border-radius: 999px;
  font-size: 15px; font-weight: 600; color: var(--muted);
}
.mode[aria-selected="true"] { background: var(--ink); color: var(--ground); }

.icon-btn {
  width: 44px; height: 44px; flex: none; display: grid; place-items: center;
  border-radius: 50%; background: var(--surface); border: 1px solid var(--line);
}

/* ---------- message ---------- */

.msg { flex: none; display: flex; flex-direction: column; gap: 4px; }

.field-wrap { position: relative; }
.field {
  display: block; width: 100%; height: 66px; resize: none;
  padding: 10px 52px 10px 14px;
  border-radius: var(--radius-sm); border: 1px solid var(--line);
  background: var(--surface-2); color: var(--ink);
  font-size: 18px; line-height: 1.3; font-weight: 500;
}
.field::placeholder { color: var(--muted); opacity: .8; }
.field-code { font-family: var(--font-mono); font-size: 16px; letter-spacing: .04em; }
.field-btn {
  position: absolute; right: 6px; top: 50%; transform: translateY(-50%);
  width: 44px; height: 44px; display: grid; place-items: center;
  border-radius: 50%; color: var(--muted);
}
.field-btn[aria-pressed="true"] { background: var(--accent); color: var(--on-accent); }

.result {
  min-height: 24px; padding: 2px 6px;
  font-family: var(--font-mono); font-size: 13px; letter-spacing: .06em; color: var(--muted);
  white-space: nowrap; overflow-x: auto; scrollbar-width: none;
}
.result::-webkit-scrollbar { display: none; }
.result[data-empty="true"] { font-family: var(--font-ui); letter-spacing: 0; opacity: .8; }
.result-words { font-size: 17px; letter-spacing: .12em; color: var(--ink); }
.result-words[data-revealed="true"] {
  font-family: var(--font-display); font-weight: 700; letter-spacing: 0; color: var(--accent);
  animation: reveal .5s cubic-bezier(.2, .9, .3, 1.3);
}
@keyframes reveal { from { transform: translateY(6px) scale(.96); opacity: 0; } to { transform: none; opacity: 1; } }

/* ---------- the deck ---------- */

.deck {
  position: relative; flex: 1 1 auto; min-height: 190px;
  display: flex; flex-direction: column;
  border-radius: var(--radius); background: var(--deck); color: var(--deck-ink);
  border: 1px solid var(--deck-line); overflow: hidden;
}

.tape-wrap { position: relative; flex: none; height: 42px; border-bottom: 1px solid var(--deck-line); }
.scope { position: absolute; inset: 0; width: 100%; height: 100%; opacity: .5; }
.scope path { stroke: var(--deck-muted); opacity: .35; }
.scope path.live { stroke: var(--mark-hot); opacity: 1; }
.tape {
  position: relative; height: 100%; display: flex; align-items: flex-end; gap: 4px;
  padding: 0 14px 9px; overflow-x: auto; scrollbar-width: none; white-space: nowrap;
}
.tape::-webkit-scrollbar { display: none; }
.tape-empty { align-self: center; padding-top: 9px; font-size: 13px; color: var(--deck-muted); }
.glyph { position: relative; display: inline-flex; align-items: flex-end; gap: 3px; padding-top: 16px; margin-right: 7px; flex: none; }
.glyph.word-start { margin-left: 14px; }
.glyph::before {
  content: attr(data-char); position: absolute; top: 3px; left: 0;
  font-family: var(--font-mono); font-size: 10px; font-weight: 700; color: var(--deck-muted);
}
.glyph[data-hot]::before { color: var(--mark-hot); }
.tape i { display: block; flex: none; height: 7px; border-radius: 4px; background: var(--mark); }
.tape i.dit { width: 7px; }
.tape i.dah { width: 21px; }
.tape i[data-received] { background: var(--deck-ink); }
.tape i[data-hot] { background: var(--mark-hot); box-shadow: 0 0 10px var(--mark-hot); }

.key-stage { flex: 1 1 auto; min-height: 0; display: grid; place-items: center; padding: 10px; container-type: size; }

.key {
  --p: 0;
  position: relative; width: 170px; height: 170px;
  width: min(100cqw, 100cqh, 260px); height: min(100cqw, 100cqh, 260px);
  border-radius: 50%; touch-action: none; user-select: none; -webkit-user-select: none;
  color: var(--key-ink);
}
.key:disabled { opacity: .5; }
.key-ring {
  position: absolute; inset: 0; border-radius: 50%;
  background: conic-gradient(var(--key-ring) calc(var(--p) * 360deg), var(--deck-line) 0);
}
.key-cap {
  position: absolute; inset: 7px; border-radius: 50%;
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 6px;
  background: var(--key);
  box-shadow: inset 0 2px 0 rgb(255 255 255 / .08), inset 0 -10px 24px rgb(0 0 0 / .28), 0 6px 18px rgb(0 0 0 / .3);
  transition: transform .06s ease-out;
}
.key:active .key-cap, .key.is-down .key-cap { transform: scale(.965); }
.lamp { width: 9px; height: 9px; border-radius: 50%; background: var(--deck-line); }
.lamp.is-on { background: var(--mark-hot); box-shadow: 0 0 14px 3px var(--mark-hot); }
.key-icon { width: 34%; height: 34%; display: none; }
body[data-deck="play"][data-phase="idle"] .key-icon-play { display: block; margin-left: 5%; }
body[data-deck="play"][data-phase="tx"] .key-icon-stop { display: block; }
body[data-deck="tap"] .key-icon-tap { display: flex; align-items: center; justify-content: center; gap: 8%; }
.key-icon-tap i { width: 16%; aspect-ratio: 1; border-radius: 50%; background: currentColor; }
.key-icon-tap b { width: 46%; height: 16%; border-radius: 99px; background: currentColor; }
.key-label { font-family: var(--font-mono); font-size: 11px; font-weight: 700; letter-spacing: .2em; opacity: .75; }

.deck-btn {
  position: absolute; bottom: 8px; min-height: 44px; padding: 0 12px;
  border-radius: 999px; border: 1px solid var(--deck-line); background: var(--deck);
  color: var(--deck-ink); font-size: 12px; font-weight: 600; line-height: 1.1;
}
.deck-mode { left: 8px; max-width: 84px; text-align: center; }
.deck-mode[aria-pressed="true"] { background: var(--key-ring); border-color: var(--key-ring); color: var(--deck); }
.deck-erase { right: 8px; width: 44px; padding: 0; display: grid; place-items: center; }

/* ---------- sound ---------- */

.sound { flex: none; display: flex; flex-direction: column; gap: 6px; }

.styles { display: grid; grid-template-columns: repeat(3, 1fr); gap: 4px; padding: 3px; border-radius: var(--radius-sm); background: var(--surface); border: 1px solid var(--line); }
.seg {
  min-height: 40px; padding: 0 10px; border-radius: calc(var(--radius-sm) - 4px);
  font-size: 15px; font-weight: 600; color: var(--muted);
}
.seg[aria-pressed="true"] { background: var(--accent); color: var(--on-accent); }

.sub { height: 44px; display: flex; align-items: center; }
.bands {
  display: flex; gap: 6px; width: 100%; height: 100%; align-items: center;
  overflow-x: auto; scrollbar-width: none; scroll-snap-type: x proximity;
  margin: 0 -12px; padding: 0 12px; width: calc(100% + 24px);
}
.bands::-webkit-scrollbar { display: none; }
.chip {
  flex: none; min-height: 40px; padding: 0 14px; border-radius: 999px;
  border: 1px solid var(--line); background: var(--surface);
  font-size: 14px; font-weight: 600; white-space: nowrap; scroll-snap-align: start;
}
.chip[aria-pressed="true"] { background: var(--ink); border-color: var(--ink); color: var(--ground); }
.sub-slider { width: 100%; }
.sub-note { font-size: 13px; color: var(--muted); padding: 0 4px; }

/* ---------- speed and volume ---------- */

.controls { flex: none; display: grid; grid-template-columns: 1fr 1fr; gap: 14px; }
.slider { display: flex; flex-direction: column; gap: 0; min-width: 0; }
.slider span { font-family: var(--font-mono); font-size: 10px; font-weight: 700; letter-spacing: .12em; color: var(--muted); white-space: nowrap; }
input[type="range"] { width: 100%; height: 30px; margin: 0; accent-color: var(--accent); background: transparent; }

/* ---------- send bar ---------- */

.bar { flex: none; display: flex; gap: 6px; }
.act {
  flex: none; width: 54px; min-height: 54px;
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px;
  border-radius: var(--radius-sm); border: 1px solid var(--line); background: var(--surface);
  font-size: 10.5px; font-weight: 600; color: var(--ink); text-align: center;
}
.act-main {
  flex: 1 1 auto; width: auto; flex-direction: row; gap: 8px;
  background: var(--accent); border-color: var(--accent); color: var(--on-accent);
  font-size: 17px; font-weight: 700;
}
.act-wide { flex: 1 1 auto; width: auto; font-size: 15px; padding: 0 10px; }

.foot { display: none; flex: none; justify-content: space-between; font-family: var(--font-mono); font-size: 10px; letter-spacing: .12em; color: var(--muted); padding: 0 4px; text-transform: uppercase; }
@media (min-height: 780px) { .foot { display: flex; } }

/* ---------- sheets ---------- */

.sheet-back {
  position: fixed; inset: 0; z-index: 20; display: flex; align-items: flex-end; justify-content: center;
  background: rgb(0 0 0 / .5);
}
.sheet {
  width: 100%; max-width: 480px; max-height: 88%; overflow-y: auto;
  display: flex; flex-direction: column; gap: 14px;
  padding: 16px 16px max(18px, env(safe-area-inset-bottom));
  border-radius: var(--radius) var(--radius) 0 0;
  background: var(--ground); color: var(--ink); border: 1px solid var(--line); border-bottom: 0;
  animation: rise .22s ease-out;
}
@keyframes rise { from { transform: translateY(24px); opacity: 0; } to { transform: none; opacity: 1; } }
.sheet-head { display: flex; align-items: center; justify-content: space-between; }
.sheet-head h2 { font-family: var(--font-display); font-size: 22px; letter-spacing: -.02em; }
.how { padding-left: 20px; display: grid; gap: 8px; font-size: 15px; }
.sheet-row { display: flex; align-items: center; justify-content: space-between; gap: 12px; min-height: 48px; }
.sheet-label { font-weight: 600; }
.sheet-row .seg { background: var(--surface); border: 1px solid var(--line); color: var(--ink); min-width: 84px; min-height: 44px; }
.skin-switch { display: flex; gap: 6px; }
.skin-btn {
  width: 48px; height: 44px; border-radius: var(--radius-sm); border: 1px solid var(--line);
  background: var(--surface); font-weight: 700;
}
.skin-btn[aria-pressed="true"] { background: var(--ink); border-color: var(--ink); color: var(--ground); }
.fine { font-size: 13px; color: var(--muted); }
.fine[data-state="error"] { color: #B42318; font-weight: 600; }
html[data-theme="dark"] .fine[data-state="error"] { color: #FF8A7A; }
.sheet audio { width: 100%; height: 44px; }
.sheet-actions { display: flex; gap: 8px; }

.toast {
  position: fixed; left: 50%; bottom: calc(84px + env(safe-area-inset-bottom)); z-index: 30;
  transform: translate(-50%, 12px); max-width: min(92vw, 440px);
  padding: 10px 16px; border-radius: 999px; background: var(--ink); color: var(--ground);
  font-size: 14px; font-weight: 600; text-align: center;
  opacity: 0; pointer-events: none; transition: opacity .2s, transform .2s;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }

/* ---------- larger screens: the app sits as a phone-sized card ---------- */

@media (min-width: 560px) and (min-height: 700px) {
  .app {
    top: 50%; bottom: auto; height: min(820px, 94vh); transform: translateY(-50%);
    padding: 16px; border-radius: 34px; border: 1px solid var(--line);
    background: var(--ground); box-shadow: 0 30px 80px rgb(0 0 0 / .22);
  }
  body { background: var(--backdrop, var(--surface)); }
}

@media (max-height: 600px) {
  .field { height: 48px; font-size: 16px; }
  .deck { min-height: 160px; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation: none !important; transition: none !important; }
}
