:root {
  --ink: #171a2e;
  --panel: #222747;
  --line: #434b7c;
  --cream: #fff1d6;
  --dim: #b4bbdf;
  --light: #ffc861;
  --coral: #ff7a59;
  --pad-opacity: 0.75;
  color-scheme: dark;
}

* { box-sizing: border-box; margin: 0; }
[hidden] { display: none !important; }

html, body {
  height: 100%;
  overflow: hidden;
  overscroll-behavior: none;
  background: var(--ink);
  color: var(--cream);
  font: 400 16px/1.5 "Noto Sans Thai", "Leelawadee UI", Thonburi, system-ui, sans-serif;
  -webkit-text-size-adjust: 100%;
  -webkit-tap-highlight-color: transparent;
}

#app {
  position: fixed;
  inset: 0;
  overflow: hidden;
  padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
}

#game, #strip, #pads, #touch { position: absolute; }
#game { display: block; }
/* Must stay rendered, or browsers stop running the emulator inside it. */
#emu { position: absolute; left: 0; top: 0; opacity: 0; pointer-events: none; }
#emu iframe { display: block; border: 0; }
#pick { position: absolute; translate: -50% -50%; }
#pads, #touch { inset: 0; }
#pads { pointer-events: none; }
#touch { touch-action: none; }

#strip {
  left: 0;
  right: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--dim);
  font-size: 14px;
  font-variant-numeric: tabular-nums;
  pointer-events: none;
}

/* ---- on-screen controls ---- */

.pad {
  position: absolute;
  left: 0;
  top: 0;
  display: grid;
  place-items: center;
  border-radius: 999px;
  opacity: var(--pad-opacity);
  will-change: transform;
}
.base { border: 2px solid var(--dim); background: rgb(67 75 124 / 0.35); }
.base svg { width: 100%; fill: var(--dim); }
.base.idle { border-style: dashed; }
.knob { background: var(--cream); }
.btn {
  border: 2px solid var(--cream);
  background: rgb(34 39 71 / 0.8);
  color: var(--cream);
  font-size: 24px;
  font-weight: 700;
}
.btn.combo, .btn.small { font-size: 14px; }
.dual .btn:not(.small) { font-size: 20px; }
.btn.on { background: var(--light); border-color: var(--light); color: var(--ink); opacity: 1; }
.btn.latched { box-shadow: 0 0 0 4px var(--coral); }
.show-hit .btn::after {
  content: "";
  position: absolute;
  inset: calc(-1 * var(--grow));
  border: 1px dashed var(--coral);
  border-radius: 999px;
}
.pad.selected { outline: 3px solid var(--light); outline-offset: 4px; opacity: 1; }

/* ---- buttons and menus ---- */

button {
  min-height: 44px;
  padding: 0 14px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: var(--panel);
  color: var(--cream);
  font: inherit;
  font-weight: 600;
  touch-action: manipulation;
}
button.primary, button[aria-pressed="true"] { background: var(--light); border-color: var(--light); color: var(--ink); }
:focus-visible { outline: 3px solid var(--coral); outline-offset: 2px; }

#tabs, #open { position: absolute; top: calc(env(safe-area-inset-top) + 4px); }
#tabs { left: calc(env(safe-area-inset-left) + 8px); display: flex; gap: 4px; }
#tabs button { padding: 0 9px; font-size: 14px; }
#open { right: calc(env(safe-area-inset-right) + 8px); padding: 0 10px; font-size: 14px; }
.landscape #tabs { display: none; }
.landscape #open { opacity: 0.8; }
/* R sits in the top corner here, so the menu moves between Select and Start. */
.landscape.dual #open {
  top: auto;
  bottom: calc(env(safe-area-inset-bottom) + 8px);
  right: auto;
  left: calc(50% + (env(safe-area-inset-left) - env(safe-area-inset-right)) / 2);
  translate: -50% 0;
  padding: 0 5px;
  font-size: 13px;
}
.bare #pads, .bare #tabs, .bare #open { display: none; }

#toast {
  position: absolute;
  top: calc(env(safe-area-inset-top) + 12px);
  left: 50%;
  translate: -50% 0;
  width: max-content;
  max-width: calc(100% - 32px);
  padding: 8px 14px;
  border-radius: 10px;
  background: var(--cream);
  color: var(--ink);
  font-size: 15px;
  text-align: center;
  pointer-events: none;
}
.box {
  position: absolute;
  top: 50%;
  left: 50%;
  translate: -50% -50%;
  width: min(340px, calc(100% - 32px));
  padding: 20px;
  border: 1px solid var(--line);
  border-radius: 14px;
  background: var(--ink);
  text-align: center;
}
#lost button { margin-top: 14px; }
#load .actions { justify-content: center; }
.editing #pick { display: none; }
summary { min-height: 44px; padding: 10px 0; font-weight: 600; cursor: pointer; }
#padmap { display: grid; grid-template-columns: repeat(auto-fill, minmax(140px, 1fr)); gap: 6px; margin-top: 8px; }
#padmap button { display: flex; justify-content: space-between; align-items: center; gap: 6px; padding: 0 10px; font-size: 14px; }
#padmap span { color: var(--dim); font-weight: 400; }
#padmap [aria-pressed="true"] span { color: inherit; }
@media (max-width: 374px) {
  #tabs button, #open { padding: 0 5px; font-size: 13px; }
}

#editbar {
  position: absolute;
  top: calc(env(safe-area-inset-top) + 4px);
  left: 50%;
  translate: -50% 0;
  width: min(420px, calc(100% - 16px - env(safe-area-inset-left) - env(safe-area-inset-right)));
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
  padding: 10px 12px;
  border: 1px solid var(--line);
  border-radius: 14px;
  background: var(--ink);
}
#editbar label { display: flex; align-items: center; gap: 8px; }
#editbar input { min-width: 0; flex: 1; }
.editing #tabs, .editing #open { display: none; }

dialog {
  width: min(560px, 100%);
  max-width: none;
  height: 100%;
  max-height: none;
  margin: 0 auto;
  padding: env(safe-area-inset-top) env(safe-area-inset-right) 0 env(safe-area-inset-left);
  border: 0;
  background: var(--ink);
  color: var(--cream);
  overflow: hidden;
}
dialog[open] { display: flex; flex-direction: column; }
dialog::backdrop { background: rgb(10 12 25 / 0.7); }
dialog header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 8px 16px;
  border-bottom: 1px solid var(--line);
}
h1 { font-size: 20px; }
h2 { font-size: 17px; color: var(--light); }
h3 { margin-top: 12px; font-size: 15px; font-weight: 600; }
.body {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  overscroll-behavior: contain;
  padding: 16px 16px calc(24px + env(safe-area-inset-bottom));
}
section + section { margin-top: 28px; }
.note { color: var(--dim); font-size: 14px; }
.seg { display: flex; gap: 6px; margin-top: 6px; }
.seg button { flex: 1; padding: 0 6px; font-size: 15px; }
.row { display: grid; grid-template-columns: 1fr auto; margin-top: 14px; }
.row input { grid-column: 1 / -1; width: 100%; height: 36px; }
input { accent-color: var(--light); }
.check { display: flex; align-items: center; gap: 12px; min-height: 44px; }
.check input { width: 22px; height: 22px; flex: none; }
.actions { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 10px; }
table { width: 100%; margin-top: 6px; border-collapse: collapse; font-size: 14px; font-variant-numeric: tabular-nums; }
th, td { padding: 8px 4px; border-bottom: 1px solid var(--line); text-align: right; }
th:first-child, td:first-child { text-align: left; }
th { color: var(--dim); font-weight: 600; }
