:root {
  color-scheme: dark;
  --paper: #f3ece0;
  --muted: #a5a5b5;
  --surface: #20222d;
  --line: #373947;
  --orange: #ff9b6c;
  --mint: #a9e2be;
  --field: min(46vw, calc((100dvh - 145px) * .6), 520px);
  font: 16px/1.35 "Segoe UI", system-ui, sans-serif;
  background: #161820;
  color: var(--paper);
}

* { box-sizing: border-box; }
body {
  min-width: 265px;
  min-height: 100dvh;
  margin: 0;
  display: grid;
  place-items: center;
  background:
    radial-gradient(ellipse at 48% 34%, #292b38 0%, transparent 63%),
    repeating-linear-gradient(135deg, #ffffff03 0 1px, transparent 1px 7px),
    #161820;
}
button { font: inherit; cursor: pointer; touch-action: manipulation; -webkit-tap-highlight-color: transparent; }
button:disabled { cursor: default; }
button:focus-visible { outline: 2px solid var(--mint); outline-offset: 3px; }
[hidden] { display: none !important; }

.machine { width: min(100% - 48px, 1120px); padding: 18px 0; }
.masthead {
  display: flex;
  justify-content: space-between;
  align-items: center;
  border-bottom: 1px solid var(--line);
  margin-bottom: 20px;
  padding: 0 0 11px;
}
.brand { display: flex; align-items: center; gap: 11px; }
.brand-mark {
  display: grid;
  place-items: center;
  width: 36px;
  height: 36px;
  border-radius: 8px;
  color: #241917;
  background: var(--orange);
  font-size: 26px;
  font-weight: 800;
  line-height: 1;
}
h1 { margin: 0; font-size: 29px; font-weight: 950; letter-spacing: -.055em; line-height: 1; }
h1 span { color: var(--orange); }
.icon-button {
  width: 39px;
  height: 36px;
  color: var(--paper);
  background: #252733;
  border: 1px solid var(--line);
  border-radius: 7px;
  font-size: 18px;
  line-height: 1;
}
.icon-button:disabled { opacity: .48; }
.icon-button:not(:disabled):hover { background: #383946; }
.masthead-actions { display: flex; align-items: center; gap: 6px; }
.ranking-button {
  height: 36px;
  padding: 0 10px;
  color: var(--paper);
  background: #252733;
  border: 1px solid var(--line);
  border-radius: 7px;
  font-size: 12px;
  font-weight: 750;
}
.ranking-button:hover { background: #383946; }
.ranking-dialog {
  width: min(335px, calc(100% - 24px));
  max-height: calc(100% - 24px);
  padding: 13px;
  color: var(--paper);
  background: #242631;
  border: 1px solid #555365;
  border-radius: 12px;
  box-shadow: 0 12px 32px #101119a0;
}
.ranking-dialog::backdrop { background: #10121bbf; }
.ranking-header { display: flex; align-items: center; justify-content: space-between; gap: 8px; }
.ranking-header h2 { margin: 0; font-size: 20px; letter-spacing: -.03em; }
#ranking-status { margin: 7px 0 0; color: var(--muted); font-size: 12px; }
#ranking-status:empty { display: none; }
.ranking-results { margin: 9px 0 0; padding: 0; list-style: none; }
.ranking-results li {
  display: grid;
  grid-template-columns: 2ch minmax(0, 1fr) max-content;
  gap: 8px;
  align-items: baseline;
  padding: 5px 0;
  border-top: 1px solid var(--line);
  font-size: 13px;
}
.ranking-results li > :nth-child(2) { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.ranking-results li > :first-child { color: var(--muted); }
.ranking-results strong { color: var(--orange); font-family: Consolas, monospace; }

.machine-body {
  display: grid;
  grid-template-columns: minmax(100px, 158px) var(--field) minmax(174px, 205px);
  justify-content: center;
  align-items: start;
  gap: clamp(14px, 2.6vw, 34px);
}
.stats, .systems { display: grid; gap: 9px; padding-top: 12px; min-width: 0; }
.readout, .panel {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 9px;
  padding: 13px;
}
.readout span, .panel h2 {
  display: block;
  color: var(--muted);
  font: 700 10px/1.4 system-ui, sans-serif;
  letter-spacing: .095em;
  text-transform: uppercase;
}
.readout { --readout-size: clamp(15px, 2vw, 27px); }
.readout strong {
  display: block;
  font: 800 var(--readout-size)/1.14 Consolas, "Courier New", monospace;
  margin-top: 4px;
}
.score { --readout-size: clamp(22px, 3vw, 42px); border-top-color: #ba7357; }
.score strong { color: var(--orange); }
.score, .best { container-type: inline-size; }
.score strong, .best strong {
  white-space: nowrap;
  font-size: min(var(--readout-size), calc(160cqi / var(--score-length, 1)));
  line-height: calc(var(--readout-size) * 1.14);
}
.small-readout { --readout-size: 19px; display: flex; flex-wrap: wrap; justify-content: space-between; gap: 8px; }
.small-readout strong { overflow-wrap: anywhere; }

.playfield { min-width: 0; }
.rail { display: flex; align-items: center; gap: 9px; height: 12px; }
.rail span { flex: 1; height: 1px; background: #4b4a57; }
.rail i { height: 4px; width: 24px; border-radius: 20px; background: #756154; }
.bottom-rail i { background: #495750; }
.well {
  position: relative;
  width: 100%;
  aspect-ratio: 12 / 20;
  isolation: isolate;
  border: 1px solid #535363;
  background: #171924;
  border-radius: 12px;
  overflow: hidden;
  box-shadow: 0 12px 32px #090a10aa;
}
#board { display: block; width: 100%; height: 100%; }
#board:focus { outline: none; }
.screen {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  padding: 12px;
  background: #10121b72;
}
.screen-card {
  width: min(100%, 270px);
  text-align: center;
  padding: clamp(13px, 3.8vw, 25px) 12px;
  background: #242631f7;
  border: 1px solid #555365;
  border-radius: 12px;
  box-shadow: 0 12px 32px #101119a0;
}
.screen-mark {
  color: var(--orange);
  font: 800 clamp(26px, 5vw, 48px)/.9 system-ui, sans-serif;
  margin-bottom: 9px;
}
.screen h2 { margin: 0; font-size: clamp(19px, 3vw, 29px); font-weight: 850; letter-spacing: -.04em; }
.screen p { color: #c1becc; font-size: 13px; margin: 8px 0 17px; }
.primary {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 12px;
  padding: 10px 14px;
  border: 1px solid #ffc099;
  border-radius: 7px;
  color: #281b18;
  background: var(--orange);
  font-size: 13px;
  font-weight: 800;
  box-shadow: 0 3px #a35134;
}
.primary:hover { background: #ffb691; }
.primary:active { box-shadow: none; transform: translateY(2px); }
.event {
  position: absolute;
  left: 50%;
  bottom: 12px;
  width: max-content;
  max-width: calc(100% - 20px);
  margin: 0;
  padding: 5px 10px;
  transform: translateX(-50%);
  background: #171924f5;
  border: 1px solid #526d61;
  border-radius: 6px;
  color: var(--mint);
  text-align: center;
  font-size: 12px;
  pointer-events: none;
}
.event:empty { opacity: 0; }
.event[data-drehtris="true"] {
  color: #281b18;
  border-color: #ffc099;
  background: var(--orange);
  font-weight: 850;
}

.panel h2 { margin: 0 0 8px; }
.previews {
  --preview-height: 60px;
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 8px;
}
.previews h2 { display: flex; align-items: center; gap: 3px; min-height: 18px; }
.previews canvas, .hold-button {
  display: block;
  width: 100%;
  height: var(--preview-height);
  border: 1px solid #353848;
  border-radius: 6px;
  background: #171924;
}
.previews canvas { object-fit: contain; }
.hold-button { padding: 0; }
.hold-button canvas { height: 100%; border: 0; background: transparent; }
.hold-button:not(:disabled):hover { border-color: var(--mint); }
.hold-button:disabled canvas { opacity: .56; }
.meter { display: grid; grid-template-columns: 1fr 1fr; gap: 5px; }
.meter i { height: 6px; border-radius: 3px; background: #414454; }
.meter[data-charge="1"] i:first-child, .meter[data-charge="2"] i { background: var(--mint); }
.turn p { margin: 6px 0 9px; color: var(--muted); font-size: 11px; }
.turn-button {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 5px;
  width: 100%;
  padding: 5px 4px;
  border: 1px solid #556c61;
  border-radius: 6px;
  background: #2c4239;
  color: #e1f3e8;
  font-size: 11px;
  font-weight: 750;
  min-height: 34px;
}
.turn-button:disabled { background: #272a35; border-color: #3b3e4c; color: #a5a5b5; }
.turn-button:not(:disabled):hover, .turn-button[aria-pressed="true"] { background: #416452; }
.turn-symbol { font-size: 16px; line-height: 1; }
kbd {
  display: inline-block;
  min-width: 18px;
  padding: 1px 3px;
  text-align: center;
  color: var(--paper);
  font: 700 10px/1.4 Consolas, monospace;
  border: 1px solid #505366;
  background: #292c38;
  border-radius: 4px;
}
.keys { display: grid; gap: 6px; color: #c1becc; font-size: 11px; }
.keys div { display: grid; grid-template-columns: minmax(63px, auto) 1fr; gap: 4px; align-items: center; }
.touch-controls { display: none; }

@media (max-width: 730px) {
  .machine { width: min(100% - 24px, 440px); padding: 8px 0; }
  .masthead { margin-bottom: 7px; padding-bottom: 6px; }
  .brand-mark, .icon-button { width: 30px; height: 30px; }
  .ranking-button { height: 30px; }
  .brand-mark { font-size: 23px; }
  h1 { font-size: 25px; }
  .machine-body { grid-template-columns: minmax(0, 1fr) minmax(112px, 1.1fr); gap: 5px 7px; }
  .stats { grid-column: 1 / -1; grid-template-columns: 1fr 1fr 1.2fr; padding: 0; gap: 5px; }
  .systems { grid-row: 3; grid-column: 1 / -1; grid-template-columns: subgrid; padding: 0; gap: 7px; }
  .playfield { grid-column: 1 / -1; width: min(100%, max(136px, calc((100dvh - 251px) * .6))); justify-self: center; }
  .readout, .panel { padding: 6px 8px; }
  .readout { --readout-size: 18px; }
  .readout strong { margin-top: 2px; }
  .readout span, .panel h2 { font-size: 9px; }
  .rail { height: 6px; }
  .bottom-rail { display: none; }
  .keys { display: none; }
  .previews { --preview-height: 38px; gap: 4px; }
  .previews h2 { margin-bottom: 3px; }
  .turn { display: grid; grid-template-columns: 1fr auto; gap: 3px 6px; align-items: center; }
  .turn h2, .turn p { margin: 0; }
  .turn h2 { grid-column: 1 / -1; }
  .turn-button { grid-column: 1 / -1; min-height: 25px; padding: 2px 4px; }
  .turn-button kbd { display: none; }
}

@media (pointer: coarse), (max-width: 730px), (max-height: 440px) and (min-aspect-ratio: 3/2) {
  .touch-controls { display: grid; grid-column: 1 / -1; grid-template-columns: repeat(5, 1fr); gap: 5px; }
  .touch-controls button {
    min-height: 42px;
    padding: 0;
    color: var(--paper);
    background: #262936;
    border: 1px solid #494c5d;
    border-radius: 7px;
    font-size: 21px;
    font-weight: 800;
    user-select: none;
    touch-action: none;
  }
  .touch-controls button:active { background: #484b5f; }
  .touch-controls button:disabled { color: #a5a5b5; }
  .touch-controls button:last-child { color: var(--orange); }
}

@media (max-height: 440px) and (min-aspect-ratio: 3/2) {
  .machine { width: min(100% - 12px, 960px); padding: 3px 0; }
  .masthead { margin-bottom: 4px; padding-bottom: 3px; }
  .brand { gap: 6px; }
  .brand-mark, .icon-button { width: 24px; height: 22px; font-size: 16px; }
  .ranking-button { height: 22px; padding: 0 6px; font-size: 10px; }
  h1 { font-size: clamp(16px, 4vw, 24px); }
  .machine-body { grid-template-columns: minmax(0, .7fr) min(calc((100dvh - 40px) * .6), 46vw) minmax(0, 1fr); gap: clamp(5px, 2vw, 14px); }
  .playfield { grid-column: 2; grid-row: 1; width: 100%; }
  .rail, .keys { display: none; }
  .stats, .systems { grid-row: 1; grid-template-columns: 1fr; padding: 0; gap: 3px; }
  .stats { grid-column: 1; }
  .systems { grid-column: 3; }
  .readout, .panel { padding: 4px; border-radius: 6px; }
  .readout { --readout-size: clamp(12px, 2.6vw, 23px); }
  .readout span, .panel h2 { font-size: clamp(7px, 1.5vw, 10px); }
  .small-readout { display: grid; grid-template-columns: auto auto; gap: 2px; }
  .previews { --preview-height: clamp(20px, 10dvh, 48px); gap: 4px; padding: 3px 4px; }
  .previews h2 { min-height: 10px; margin-bottom: 2px; }
  .previews kbd { display: none; }
  .turn { display: grid; grid-template-columns: 1fr auto; align-items: center; gap: 2px 3px; }
  .turn h2, .turn p { margin: 0; }
  .turn h2, .turn-button { grid-column: 1 / -1; }
  .turn p { font-size: clamp(7px, 1.4vw, 11px); line-height: 1.15; }
  .meter { gap: 3px; }
  .meter i { height: 4px; }
  .turn-button { min-height: 20px; padding: 0 2px; font-size: clamp(8px, 1.45vw, 11px); gap: 3px; }
  .turn-symbol { font-size: 13px; }
  .turn-button kbd { display: none; }
  .touch-controls { grid-template-columns: repeat(6, 1fr); gap: 3px; }
  .touch-controls button { grid-column: span 2; min-height: clamp(21px, 10dvh, 42px); font-size: clamp(14px, 3vw, 21px); }
  .touch-controls button:nth-last-child(-n + 2) { grid-column: span 3; }
  .screen { padding: 4px; }
  .screen-card { padding: clamp(4px, 3dvh, 20px) 4px; border-radius: 7px; }
  .screen-mark { font-size: clamp(16px, 9dvh, 38px); margin-bottom: 3px; }
  .screen h2 { font-size: clamp(15px, 6dvh, 24px); }
  .screen p { font-size: clamp(9px, 3dvh, 12px); margin: 4px 0 7px; }
  .primary { font-size: clamp(9px, 3.2dvh, 12px); padding: clamp(4px, 2dvh, 8px); gap: 4px; }
  .event { bottom: 3px; font-size: 9px; padding: 3px; }
}
