:root {
  color-scheme: dark;
  --page-bg: #050505;
  --frame: #111111;
  --frame-edge: #010101;
  --frame-highlight: rgba(255, 255, 255, 0.14);
  --tile-bg-light: #262626;
  --tile-bg: #171717;
  --tile-bg-darker: #080808;
  --tile-text: #f2eee4;
  --tile-highlight: rgba(255, 255, 255, 0.16);
  --tile-split: rgba(255, 255, 255, 0.08);
  --tile-shadow: rgba(0, 0, 0, 0.72);
  --tile-radius: 2px;
  --accent: #c86532;
  --accent-quiet: #5f7f87;
  --panel: rgba(14, 14, 14, 0.9);
  --panel-border: rgba(242, 238, 228, 0.16);
  --text-muted: rgba(242, 238, 228, 0.68);
  --shadow: rgba(0, 0, 0, 0.76);
  font-family: ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  font-size: 16px;
  letter-spacing: 0;
}

:root[data-theme="chapel"] {
  --page-bg: #0b0a08;
  --frame: #15130f;
  --frame-edge: #030302;
  --frame-highlight: rgba(211, 171, 111, 0.2);
  --tile-bg-light: #28241e;
  --tile-bg: #181511;
  --tile-bg-darker: #080705;
  --tile-text: #f3ead7;
  --accent: #b78a4b;
  --accent-quiet: #748e80;
}

:root[data-theme="daylight"] {
  color-scheme: light;
  --page-bg: #d7d4cb;
  --frame: #141414;
  --frame-edge: #050505;
  --frame-highlight: rgba(255, 255, 255, 0.22);
  --tile-bg-light: #252525;
  --tile-bg: #161616;
  --tile-bg-darker: #090909;
  --tile-text: #f5f1e7;
  --panel: rgba(19, 19, 19, 0.9);
  --text-muted: rgba(245, 241, 231, 0.72);
  --shadow: rgba(28, 26, 22, 0.34);
}

* {
  box-sizing: border-box;
}

html,
body {
  width: 100%;
  min-width: 320px;
  height: 100%;
  margin: 0;
  overflow: hidden;
  background: var(--page-bg);
  color: var(--tile-text);
}

button,
select,
input {
  font: inherit;
}

.stage {
  position: relative;
  display: grid;
  width: 100%;
  height: 100%;
  place-items: center;
  padding: clamp(14px, 3vw, 48px);
}

.display-shell {
  display: grid;
  width: min(calc(100vw - clamp(28px, 6vw, 96px)), calc(82vh * 1.96));
}

.board-frame {
  position: relative;
  padding: clamp(12px, 1.8vw, 46px) clamp(14px, 2.1vw, 58px);
  border: 1px solid var(--frame-highlight);
  border-radius: 10px;
  background:
    linear-gradient(180deg, rgba(255, 255, 255, 0.055), transparent 18%),
    linear-gradient(145deg, var(--frame), var(--frame-edge));
  box-shadow:
    0 40px 88px var(--shadow),
    inset 0 1px 0 rgba(255, 255, 255, 0.11),
    inset 0 -24px 42px rgba(0, 0, 0, 0.44);
}

.board-frame::before {
  position: absolute;
  inset: clamp(5px, 0.75vw, 10px);
  pointer-events: none;
  content: "";
  border: 1px solid rgba(242, 238, 228, 0.055);
  border-radius: 7px;
}

.board-frame::after {
  position: absolute;
  top: clamp(5px, 0.75vw, 10px);
  right: clamp(30px, 4vw, 68px);
  left: clamp(30px, 4vw, 68px);
  height: 1px;
  pointer-events: none;
  content: "";
  background: linear-gradient(90deg, transparent, rgba(200, 101, 50, 0.72), transparent);
}

.split-flap-board {
  display: grid;
  grid-template-columns: repeat(var(--columns), minmax(0, 1fr));
  grid-template-rows: repeat(var(--rows), minmax(0, 1fr));
  gap: clamp(2px, 0.32vw, 9px);
  /* Matches real Vestaboard Flagship: tile bit is 1.43" x 2.68", so tileW/tileH = 0.534, and board = 22 * 0.534 / 6 ≈ 1.96. */
  aspect-ratio: 1.96;
  perspective: 720px;
}

.flap-tile {
  position: relative;
  display: block;
  min-width: 0;
  min-height: 0;
  overflow: hidden;
  border-radius: var(--tile-radius);
  background: var(--tile-bg);
  box-shadow:
    inset 0 1px 0 var(--tile-highlight),
    inset 0 -1px 0 rgba(0, 0, 0, 0.88),
    0 1px 1px rgba(255, 255, 255, 0.035),
    0 2px 5px rgba(0, 0, 0, 0.68);
  contain: layout paint;
  transform-style: preserve-3d;
}

.flap-tile::before {
  position: absolute;
  top: calc(50% - 1px);
  right: 0;
  left: 0;
  z-index: 7;
  height: 2px;
  pointer-events: none;
  content: "";
  background: rgba(0, 0, 0, 0.62);
  box-shadow:
    0 -1px 0 rgba(255, 255, 255, 0.045),
    0 1px 0 rgba(255, 255, 255, 0.035);
}

.flap-tile::after {
  position: absolute;
  right: 9%;
  bottom: 8%;
  left: 9%;
  z-index: 7;
  height: 1px;
  pointer-events: none;
  content: "";
  background: rgba(255, 255, 255, 0.045);
}

.flap-static,
.flap-blade {
  position: absolute;
  right: 0;
  left: 0;
  overflow: hidden;
  backface-visibility: hidden;
}

.flap-static {
  z-index: 1;
}

.top-half {
  top: 0;
  height: 50%;
  border-radius: var(--tile-radius) var(--tile-radius) 0 0;
  background: linear-gradient(180deg, var(--tile-bg-light), var(--tile-bg));
  box-shadow: inset 0 1px 0 var(--tile-highlight);
}

.bottom-half {
  bottom: 0;
  height: 50%;
  border-radius: 0 0 var(--tile-radius) var(--tile-radius);
  background: linear-gradient(180deg, var(--tile-bg), var(--tile-bg-darker));
  box-shadow: inset 0 -1px 0 rgba(0, 0, 0, 0.72);
}

.flap-blade {
  height: 50%;
  transform-style: preserve-3d;
}

.flap-front {
  top: 0;
  z-index: 4;
  border-radius: var(--tile-radius) var(--tile-radius) 0 0;
  background: linear-gradient(180deg, var(--tile-bg-light), var(--tile-bg));
  box-shadow: inset 0 1px 0 var(--tile-highlight), 0 1px 3px var(--tile-shadow);
  transform: rotateX(0deg);
  transform-origin: bottom center;
}

.flap-back {
  bottom: 0;
  z-index: 3;
  border-radius: 0 0 var(--tile-radius) var(--tile-radius);
  background: linear-gradient(180deg, var(--tile-bg), var(--tile-bg-darker));
  box-shadow: inset 0 1px 0 var(--tile-split), 0 1px 3px var(--tile-shadow);
  transform: rotateX(90deg);
  transform-origin: top center;
}

.tile-character {
  position: absolute;
  left: 0;
  display: grid;
  width: 100%;
  height: 200%;
  place-items: center;
  color: var(--tile-text);
  font-family: "Helvetica Neue", Arial, ui-sans-serif, system-ui, sans-serif;
  font-size: clamp(12px, min(1.95vw, 5.5vh), 78px);
  font-weight: 800;
  line-height: 1;
  letter-spacing: 0;
  text-align: center;
  text-shadow: 0 1px 2px rgba(0, 0, 0, 0.56);
  text-transform: uppercase;
  user-select: none;
}

/* Color-tile face. A centered rectangle (65% wide, 55% tall of the full tile) bisected by
   the hinge. Each half-tile renders its corresponding portion of the rectangle; overflow is
   clipped by the half's own `overflow: hidden`. Hidden by default; paintFace shows when the
   tile holds one of codes 63-71. */
.tile-color {
  position: absolute;
  left: 17.5%;
  width: 65%;
  display: none;
  border-radius: 1px;
  pointer-events: none;
}

.top-half .tile-color,
.flap-front .tile-color {
  top: 45%;
  height: 110%;
}

.bottom-half .tile-color,
.flap-back .tile-color {
  top: -55%;
  height: 110%;
}

.top-half .tile-character,
.flap-front .tile-character {
  top: 0;
}

.bottom-half .tile-character,
.flap-back .tile-character {
  bottom: 0;
}

.top-right-link {
  position: fixed;
  top: 16px;
  right: 16px;
  z-index: 10;
  display: inline-flex;
  gap: 8px;
  align-items: center;
  height: 34px;
  padding: 0 14px;
  color: var(--tile-text);
  font-family: ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, sans-serif;
  font-size: 11px;
  font-weight: 700;
  letter-spacing: 0.08em;
  text-decoration: none;
  text-shadow: 0 1px 1px rgba(0, 0, 0, 0.45);
  text-transform: uppercase;
  background: rgba(38, 38, 38, 0.78);
  border: 1px solid rgba(242, 238, 228, 0.18);
  border-radius: 6px;
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.32);
  backdrop-filter: blur(12px);
  transition: border-color 200ms ease, color 200ms ease, transform 200ms ease, box-shadow 200ms ease;
}

.top-right-link:hover {
  border-color: rgba(200, 101, 50, 0.75);
  color: #ffffff;
  transform: translateY(-1px);
  box-shadow: 0 6px 22px rgba(200, 101, 50, 0.18);
}

.top-right-link:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

.top-right-link-arrow {
  display: inline-block;
  transition: transform 200ms ease;
}

.top-right-link:hover .top-right-link-arrow {
  transform: translateX(2px);
}

.top-right-link--back:hover .top-right-link-arrow {
  transform: translateX(-2px);
}

.control-overlay {
  position: fixed;
  right: 18px;
  bottom: 18px;
  left: 18px;
  display: grid;
  gap: 10px;
  justify-items: center;
  opacity: 1;
  transform: translateY(0);
  transition: opacity 180ms ease, transform 180ms ease;
}

.control-overlay.is-hidden {
  pointer-events: none;
  opacity: 0;
  transform: translateY(12px);
}

.control-bar {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  align-items: center;
  justify-content: center;
  max-width: min(980px, 100%);
  padding: 8px;
  border: 1px solid var(--panel-border);
  border-radius: 8px;
  background: var(--panel);
  box-shadow: 0 18px 44px rgba(0, 0, 0, 0.38);
  backdrop-filter: blur(18px);
}

.control-bar button,
.control-bar select,
.switch {
  height: 34px;
  border: 1px solid rgba(242, 238, 228, 0.18);
  border-radius: 6px;
  background: rgba(242, 238, 228, 0.08);
  color: var(--tile-text);
}

.control-bar button {
  min-width: 38px;
  padding: 0 10px;
  cursor: pointer;
}

.control-bar button:hover,
.control-bar select:hover,
.switch:hover {
  border-color: rgba(200, 101, 50, 0.7);
}

.control-bar label {
  display: flex;
  gap: 6px;
  align-items: center;
  color: var(--text-muted);
  font-size: 12px;
  text-transform: uppercase;
}

.control-bar select {
  padding: 0 28px 0 10px;
}

.switch {
  padding: 0 10px;
}

.switch input {
  accent-color: var(--accent);
}

.source-panel {
  display: flex;
  flex-wrap: wrap;
  gap: 8px 14px;
  max-width: min(980px, 100%);
  padding: 10px 12px;
  border: 1px solid var(--panel-border);
  border-radius: 8px;
  background: var(--panel);
  color: var(--text-muted);
  font-size: 12px;
  line-height: 1.35;
  box-shadow: 0 18px 44px rgba(0, 0, 0, 0.38);
}

.source-panel strong {
  color: var(--tile-text);
}

.source-panel a {
  color: var(--accent);
}

@media (max-width: 760px) {
  .stage {
    padding: 10px;
  }

  .display-shell {
    width: min(94vw, calc(70vh * 1.96));
  }

  .control-bar {
    justify-content: flex-start;
    overflow-x: auto;
  }

  .top-right-link {
    top: 10px;
    right: 10px;
    height: 30px;
    padding: 0 10px;
    font-size: 10px;
    letter-spacing: 0.06em;
  }
}

@media (prefers-reduced-motion: reduce) {
  .flap-front,
  .flap-back,
  .control-overlay {
    transition: none !important;
  }
}

:root[data-saver="true"] .control-overlay,
:root[data-saver="true"] .top-right-link {
  display: none;
}

:root[data-saver="true"] .flap-front,
:root[data-saver="true"] .flap-back {
  will-change: transform;
}
