/* ==========================================================================
   Squelch — UI chrome only. All grid rendering happens on the canvas.
   Landscape-only, touch-first. No hover-dependent affordances anywhere.
   ========================================================================== */

:root {
  --bg:        #080b10;
  --panel:     #0e141c;
  --panel-2:   #131b25;
  --line:      #1e2937;
  --ink:       #d7e3ef;
  --ink-dim:   #7d8fa3;
  --accent:    #35e0d0;
  --accent-dk: #0d5f5a;
  --warn:      #ffb648;
  --good:      #5ee08d;
  --bad:       #ff5f56;

  /* Chrome heights. Kept tight — landscape phones give us ~360px of height.
     The strip holds the 44px tap floor rather than shaving pixels off it. */
  --strip-h:   44px;
  --bar-h:     52px;
  --tray-w:    56px;
  --tap:       44px;          /* minimum tap target, per touch audit */
  --radius:    8px;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  overflow: hidden;                 /* the page never scrolls; the grid scales */
  background: var(--bg);
  color: var(--ink);
  font: 500 14px/1.35 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-text-size-adjust: 100%;
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior: none;
}

body { user-select: none; -webkit-user-select: none; }

button { font-family: inherit; }

/* --------------------------------------------------------------------------
   App shell — fills the visual viewport. dvh/dvw where supported so mobile
   browser chrome collapsing/expanding doesn't leave dead space.
   -------------------------------------------------------------------------- */
#app {
  position: fixed;
  inset: 0;
  width: 100vw;
  height: 100vh;
  display: flex;
  flex-direction: column;
  padding: env(safe-area-inset-top) env(safe-area-inset-right)
           env(safe-area-inset-bottom) env(safe-area-inset-left);
}

@supports (height: 100dvh) {
  #app { width: 100dvw; height: 100dvh; }
}

/* --------------------------------------------------------------------------
   Rotate gate
   -------------------------------------------------------------------------- */
#rotate-gate {
  position: fixed;
  inset: 0;
  z-index: 100;
  display: none;
  align-items: center;
  justify-content: center;
  background: var(--bg);
  text-align: center;
  padding: 24px;
}

.rotate-inner { max-width: 320px; }
.rotate-icon  { width: 92px; height: 92px; color: var(--accent); opacity: .85; }
.rotate-inner h1 { margin: 14px 0 6px; font-size: 20px; font-weight: 700; letter-spacing: .02em; }
.rotate-inner p  { margin: 0; font-size: 14px; color: var(--ink-dim); }

@media (orientation: portrait) and (max-width: 900px) {
  #rotate-gate { display: flex; }
  #app { display: none; }
}

/* JS fallback for engines that report orientation late (mainly iOS Safari
   mid-rotation, and desktop windows resized to a portrait aspect). */
body.force-rotate-gate #rotate-gate { display: flex; }
body.force-rotate-gate #app { display: none; }

@media (prefers-reduced-motion: no-preference) {
  .rotate-icon { animation: nudge 2.4s ease-in-out infinite; }
  @keyframes nudge {
    0%, 60%, 100% { transform: rotate(0deg); }
    75%, 90%      { transform: rotate(-90deg); }
  }
}

/* --------------------------------------------------------------------------
   Info strip (top, collapsed by default)
   -------------------------------------------------------------------------- */
#info-strip {
  position: relative;
  z-index: 15;
  flex: 0 0 auto;
  background: var(--panel);
  border-bottom: 1px solid var(--line);
}

#info-toggle {
  display: flex;
  align-items: center;
  gap: 8px;
  width: 100%;
  min-height: var(--strip-h);
  padding: 0 10px;
  background: none;
  border: 0;
  color: var(--ink);
  font: inherit;
  font-size: 12.5px;
  text-align: left;
  cursor: pointer;
}

#info-toggle .chev {
  display: inline-block;
  color: var(--accent);
  transition: transform .18s ease;
  padding: 8px 4px;                 /* padded hit area, no extra strip height */
  margin: -8px -2px;
}
#info-strip:not(.collapsed) #info-toggle .chev { transform: rotate(90deg); }

#info-toggle .spacer { flex: 1; }

/* Per-tile decay is the number the whole puzzle turns on — always on show,
   not buried in the expanded panel. */
.chip {
  flex: 0 0 auto;
  padding: 3px 8px;
  border: 1px solid #3a3324;
  border-radius: 10px;
  background: rgba(255, 182, 72, .10);
  color: var(--warn);
  font-size: 11.5px;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
}
#info-title { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.stars { color: var(--warn); letter-spacing: 2px; font-size: 13px; }

/* Expanding drops a panel OVER the stage rather than displacing it, so the
   grid never rescales and tiles keep their touch size while you read. */
#info-body {
  display: block;
  position: absolute;
  left: 0; right: 0; top: 100%;
  max-height: 60vh;
  overflow-y: auto;
  overscroll-behavior: contain;
  padding: 2px 12px 10px;
  border-bottom: 1px solid var(--line);
  background: var(--panel-2);
  box-shadow: 0 10px 24px rgba(0, 0, 0, .55);
}
#info-strip.collapsed #info-body { display: none; }

.info-grid { display: flex; flex-wrap: wrap; gap: 6px 22px; margin: 8px 0 0; }
.info-grid > div { display: flex; align-items: baseline; gap: 6px; }
.info-grid dt { margin: 0; color: var(--ink-dim); font-size: 11px; text-transform: uppercase; letter-spacing: .06em; }
.info-grid dd { margin: 0; font-size: 13px; font-variant-numeric: tabular-nums; }
.hint { margin: 8px 0 0; font-size: 12px; color: var(--ink-dim); }

/* --------------------------------------------------------------------------
   Stage — canvas area + tray
   -------------------------------------------------------------------------- */
#stage { flex: 1 1 auto; display: flex; min-height: 0; }

#grid-wrap {
  flex: 1 1 auto;
  position: relative;
  min-width: 0;
  min-height: 0;
  overflow: hidden;
}

#grid {
  display: block;
  touch-action: none;               /* no scroll/zoom stealing our taps */
}

#tray {
  flex: 0 0 var(--tray-w);
  width: var(--tray-w);
  display: flex;
  flex-direction: column;
  align-items: center;
  /* Tight enough that a five-part level fits a 360px-tall phone unscrolled:
     5 x 48px + 4 gaps + padding must stay under a ~262px strip. */
  gap: 3px;
  padding: 3px;
  background: var(--panel);
  border-left: 1px solid var(--line);
  overflow-y: auto;
  overscroll-behavior: contain;
}

.tray-item {
  position: relative;
  flex: 0 0 auto;
  width: 48px;
  height: 48px;                     /* >= 44px tap target */
  display: grid;
  place-items: center;
  padding: 0;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--panel-2);
  cursor: pointer;
  touch-action: manipulation;
}
.tray-item:active { background: #1b2635; }
.tray-item.selected {
  border-color: var(--accent);
  background: var(--accent-dk);
  box-shadow: 0 0 0 1px var(--accent) inset;
}
.tray-item canvas { display: block; pointer-events: none; }

.tray-count {
  position: absolute;
  right: -2px;
  bottom: -2px;
  min-width: 17px;
  height: 17px;
  padding: 0 4px;
  border-radius: 9px;
  background: var(--line);
  color: var(--ink);
  font-size: 11px;
  font-weight: 700;
  line-height: 17px;
  text-align: center;
  pointer-events: none;
}
.tray-item.depleted { opacity: .38; }
.tray-empty { margin: 10px 0 0; color: var(--ink-dim); font-size: 10px; text-align: center; }

/* --------------------------------------------------------------------------
   Inline tile menu, tooltip, toast — all overlaid on the grid
   -------------------------------------------------------------------------- */
#tile-menu {
  position: absolute;
  z-index: 6;
  display: flex;
  gap: 4px;
  padding: 4px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: #101822;
  box-shadow: 0 6px 18px rgba(0, 0, 0, .55);
}
#tile-menu[hidden] { display: none; }

.menu-btn {
  width: var(--tap);
  height: var(--tap);
  display: grid;
  place-items: center;
  padding: 0;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--panel-2);
  color: var(--ink);
  cursor: pointer;
  touch-action: manipulation;
}
.menu-btn svg { width: 22px; height: 22px; fill: currentColor; pointer-events: none; }
.menu-btn:active { background: #22303f; }
.menu-btn.danger { color: var(--bad); }

#tooltip {
  position: absolute;
  z-index: 7;
  max-width: 230px;
  padding: 7px 10px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: rgba(10, 15, 22, .96);
  color: var(--ink);
  font-size: 12px;
  line-height: 1.45;
  white-space: pre-line;
  pointer-events: none;
  box-shadow: 0 6px 18px rgba(0, 0, 0, .55);
}
#tooltip[hidden] { display: none; }

#toast {
  position: absolute;
  z-index: 8;
  left: 50%;
  bottom: 8px;
  transform: translateX(-50%);
  padding: 6px 12px;
  border-radius: 14px;
  background: rgba(19, 27, 37, .95);
  border: 1px solid var(--line);
  color: var(--ink);
  font-size: 12px;
  white-space: nowrap;
  pointer-events: none;
}
#toast[hidden] { display: none; }

/* --------------------------------------------------------------------------
   Bottom control bar
   -------------------------------------------------------------------------- */
#controls {
  flex: 0 0 auto;
  min-height: var(--bar-h);
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 4px 10px;
  background: var(--panel);
  border-top: 1px solid var(--line);
}

.ctl {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  min-width: var(--tap);
  min-height: var(--tap);
  padding: 0 14px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--panel-2);
  color: var(--ink);
  font-size: 14px;
  font-weight: 600;
  line-height: 1;
  cursor: pointer;
  touch-action: manipulation;
}
/* display:inline-flex above would otherwise beat the UA [hidden] rule. */
.ctl[hidden] { display: none; }
.ctl:active { background: #1b2635; transform: translateY(1px); }
.ctl:disabled { opacity: .35; cursor: default; transform: none; }
.ctl.primary { background: var(--accent-dk); border-color: var(--accent); color: #eafffd; }
.ctl.primary:active { background: #0a4a46; }
.ctl-ico { width: 20px; height: 20px; fill: currentColor; }
.ctl.freq { font-variant-numeric: tabular-nums; color: var(--warn); border-color: #3a3324; }

.ctl.icon-only { padding: 0 11px; }
.ctl .unit { font-size: 11px; color: var(--ink-dim); margin-left: 3px; font-weight: 500; }
#btn-sound.muted { color: var(--ink-dim); }

/* The [hidden] attribute is an HTMLElement feature and does not hide SVG, so
   the speaker/mute swap is driven by a class on the button instead. */
#btn-sound .ctl-ico { display: none; }
#btn-sound:not(.muted) #icon-sound-on { display: block; }
#btn-sound.muted #icon-sound-off { display: block; }

/* Power meter — the signal's whole life story in one strip. */
.meter {
  flex: 1 1 auto;
  min-width: 0;
  display: flex;
  flex-direction: column;
  justify-content: center;
  gap: 4px;
  padding: 0 6px;
}
.meter-track {
  height: 7px;
  border-radius: 4px;
  background: #172230;
  box-shadow: inset 0 0 0 1px var(--line);
  overflow: hidden;
}
.meter-fill {
  display: block;
  height: 100%;
  width: 100%;
  border-radius: 4px;
  background: var(--accent);
  transition: width .09s linear, background-color .25s ease;
}
.meter-fill.good { background: var(--accent); }
.meter-fill.warn { background: var(--warn); }
.meter-fill.bad  { background: var(--bad); }

.meter-text {
  display: flex;
  align-items: baseline;
  justify-content: center;
  gap: 5px;
  font-size: 11px;
  line-height: 1;
  color: var(--ink-dim);
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
  overflow: hidden;
}
.m-strength { color: var(--accent); font-size: 15px; font-weight: 700; }
.m-range { color: var(--ink); }
.m-sep { opacity: .5; }

/* Very short landscape viewports (small phones): claw back vertical space. */
@media (max-height: 400px) {
  :root { --bar-h: 46px; }
  .ctl { padding: 0 11px; font-size: 13px; }
  .meter-text { font-size: 10px; gap: 4px; }
  .m-strength { font-size: 13px; }
  .meter-track { height: 6px; }
  .info-grid { gap: 4px 16px; margin-top: 6px; }
  .hint { margin-top: 6px; }
}

/* Tightest bars: the separators are the first thing to go, never the numbers. */
@media (max-width: 700px) {
  .m-sep { display: none; }
  .meter { padding: 0 3px; }
  .meter-text { gap: 7px; }
}

/* Narrow viewports: the rotate button drops to icon-only, still 44px wide. */
@media (max-width: 720px) {
  #btn-rotate span { display: none; }
  #btn-rotate { padding: 0 11px; }
}

/* --------------------------------------------------------------------------
   Level select
   -------------------------------------------------------------------------- */
#screen-levels {
  position: absolute;
  inset: 0;
  z-index: 20;
  display: flex;
  flex-direction: column;
  background: var(--bg);
  padding: env(safe-area-inset-top) env(safe-area-inset-right)
           env(safe-area-inset-bottom) env(safe-area-inset-left);
}
#screen-levels[hidden] { display: none; }

.ls-head {
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 6px 12px;
  border-bottom: 1px solid var(--line);
}
.ls-head h1 {
  margin: 0;
  font-size: 19px;
  font-weight: 800;
  letter-spacing: .06em;
  color: var(--accent);
  text-transform: uppercase;
}
.ls-total { flex: 1; color: var(--ink-dim); font-size: 12px; font-variant-numeric: tabular-nums; }

#level-grid {
  flex: 1 1 auto;
  display: grid;
  grid-template-columns: repeat(8, minmax(0, 1fr));
  gap: 8px;
  padding: 10px 12px;
  overflow-y: auto;
  overscroll-behavior: contain;
  align-content: start;
  align-content: safe center;       /* centred, but never clipped if it grows */
}

.level-cell {
  min-height: var(--tap);
  aspect-ratio: 1 / 1;
  max-height: 104px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: var(--panel-2);
  color: var(--ink);
  cursor: pointer;
  touch-action: manipulation;
}
.level-cell:active { background: #1b2635; }
.level-cell.locked { opacity: .4; cursor: default; background: var(--panel); }
.lc-num { font-size: 17px; font-weight: 700; font-variant-numeric: tabular-nums; }
.lc-stars { font-size: 10px; letter-spacing: 1px; color: var(--warn); min-height: 12px; }

@media (max-height: 400px) {
  #level-grid { grid-template-columns: repeat(8, minmax(0, 1fr)); gap: 6px; padding: 8px 10px; }
  .level-cell { max-height: 72px; }
  .lc-num { font-size: 15px; }
}

/* --------------------------------------------------------------------------
   Result overlay
   -------------------------------------------------------------------------- */
#overlay-result {
  position: absolute;
  inset: 0;
  z-index: 30;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 12px;
  background: rgba(6, 9, 13, .78);
}
#overlay-result[hidden] { display: none; }

.result-card {
  width: min(420px, 100%);
  max-height: 100%;
  overflow-y: auto;
  padding: 14px 18px 16px;
  border: 1px solid var(--line);
  border-radius: 14px;
  background: var(--panel);
  text-align: center;
  box-shadow: 0 12px 40px rgba(0, 0, 0, .6);
}
.result-card h2 { margin: 0 0 4px; font-size: 19px; font-weight: 700; }
.result-card h2.win { color: var(--good); }
.result-card h2.fail { color: var(--bad); }
.stars-big { font-size: 26px; letter-spacing: 6px; color: var(--warn); line-height: 1.2; }
.stars-big[hidden] { display: none; }
.result-card p { margin: 6px 0 12px; font-size: 13px; color: var(--ink-dim); line-height: 1.5; }
.result-actions { display: flex; gap: 8px; justify-content: center; flex-wrap: wrap; }
.result-actions .ctl[hidden] { display: none; }

@media (max-height: 400px) {
  .result-card { padding: 10px 14px 12px; }
  .result-card h2 { font-size: 17px; }
  .stars-big { font-size: 22px; }
  .result-card p { margin: 4px 0 9px; }
}

/* Hover polish only — never the sole affordance for anything. */
@media (hover: hover) and (pointer: fine) {
  .ctl:not(:disabled):hover { border-color: #2c3d52; }
  .level-cell:not(.locked):hover { border-color: #2c3d52; }
}
