:root {
  color-scheme: light;
  --bg: #f5f7f8;
  --panel: #ffffff;
  --text: #17212b;
  --text-on-light: #17212b;
  --muted: #66717d;
  --line: #dbe2e8;
  --green: #16834a;
  --green-soft: #e5f6ec;
  --yellow: #9b7400;
  --yellow-soft: #fff5cf;
  --orange: #b95310;
  --orange-soft: #ffe8d7;
  --red: #c92a2a;
  --red-soft: #ffe3e3;
  --blue: #2764d8;
  --blue-soft: #e7efff;
  --violet: #6941c6;
  --violet-soft: #efe8ff;
  --shadow: 0 8px 24px rgba(26, 39, 54, 0.08);
}

* { box-sizing: border-box; }

html {
  min-width: 320px;
  -webkit-text-size-adjust: 100%;
  touch-action: manipulation;
}

body {
  margin: 0;
  min-height: 100vh;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Arial, sans-serif;
  font-size: 16px;
  letter-spacing: 0;
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
}

input,
textarea,
.reverse-box,
#sequenceSummary,
.sequence-list,
.move-card,
.numbering-prompt strong {
  -webkit-user-select: text;
  user-select: text;
  -webkit-touch-callout: default;
}

button, input, textarea, select {
  font: inherit;
}

button {
  min-height: 44px;
  border: 0;
  border-radius: 8px;
  padding: 11px 14px;
  color: var(--text);
  background: #eef2f5;
  font-weight: 700;
}

button:active { transform: translateY(1px); }
button:focus-visible, input:focus-visible, textarea:focus-visible, select:focus-visible {
  outline: 3px solid rgba(39, 100, 216, 0.35);
  outline-offset: 2px;
}

input, textarea, select {
  width: 100%;
  min-height: 44px;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 11px 12px;
  background: #fff;
  color: var(--text);
  font-size: 16px;
}

textarea { resize: vertical; }
label { display: block; margin: 12px 0 7px; font-weight: 700; }

.app-shell {
  width: min(100%, 760px);
  margin: 0 auto;
  padding: calc(14px + env(safe-area-inset-top)) 12px calc(98px + env(safe-area-inset-bottom));
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 12px;
}

.eyebrow {
  margin: 0 0 3px;
  color: var(--muted);
  font-size: 12px;
  font-weight: 800;
  text-transform: uppercase;
}

h1, h2, p { margin: 0; }
h1 { font-size: 24px; line-height: 1.12; }
h2 { font-size: 18px; line-height: 1.2; }

.current-card {
  margin-top: 6px;
  color: var(--green);
  font-size: 15px;
  font-weight: 900;
}

.panel {
  margin: 12px 0;
  padding: 14px;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 8px;
  box-shadow: var(--shadow);
}

.section-head {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 12px;
}

.section-head p { color: var(--muted); font-size: 14px; margin-top: 3px; }

.save-top, .counter {
  flex: 0 0 auto;
  padding: 7px 10px;
  border-radius: 999px;
  background: var(--blue-soft);
  color: #164aa7;
  font-size: 13px;
  font-weight: 800;
}

.tray {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 8px;
}

.tray.is-danger { animation: shake 260ms ease-in-out; }

.tray-cell {
  min-height: 98px;
  border: 1px dashed #bec8d2;
  border-radius: 8px;
  background: #f9fbfc;
  display: flex;
  align-items: center;
  justify-content: center;
  text-align: center;
  overflow: hidden;
}

.tray-cell.danger-zone {
  border-color: #ff0000;
  background: #ff0000;
  color: #ffffff;
  font-size: 13px;
  font-weight: 900;
}

.tile {
  width: 100%;
  height: 100%;
  min-height: 98px;
  padding: 8px;
  border-radius: 8px;
  border: 2px solid var(--blue);
  background: var(--blue-soft);
  display: flex;
  flex-direction: column;
  justify-content: center;
  gap: 4px;
  animation: pop 180ms ease-out;
}

.tile.part-2 {
  border-color: var(--violet);
  background: var(--violet-soft);
}

.tile strong { font-size: 25px; line-height: 1; }
.tile span { font-size: 12px; color: var(--muted); }
.period-bar { height: 5px; border-radius: 999px; background: rgba(39, 100, 216, 0.2); overflow: hidden; }
.period-bar i { display: block; height: 100%; width: var(--w, 20%); background: currentColor; }
.tile-action {
  min-height: 34px;
  margin-top: 3px;
  padding: 6px 7px;
  border: 1px solid rgba(39, 100, 216, 0.25);
  background: #fff;
  color: #164aa7;
  font-size: 11px;
  line-height: 1.1;
}

.message {
  min-height: 22px;
  margin-top: 10px;
  color: var(--muted);
  font-weight: 700;
}
.message.error { color: var(--red); }
.message.ok { color: var(--green); }

.mode-tabs {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
  margin-bottom: 12px;
}
.tab.is-active { background: var(--blue); color: #fff; }
.mode-block { display: none; }
.mode-block.is-active { display: block; }

.next-card-box {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  min-height: 64px;
  padding: 12px 20px 12px 12px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #f6f8fa;
}
.next-card-box span {
  color: var(--muted);
  font-weight: 800;
}
.next-card-box strong {
  font-size: 30px;
  line-height: 1;
  min-width: 42px;
  text-align: center;
}

.split-actions {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
  margin-top: 10px;
}

.primary { background: var(--green); color: #fff; }
.secondary { background: var(--blue); color: #fff; }
.ghost { background: #eef2f5; }
.danger { background: var(--red-soft); color: var(--red); }
.full { width: 100%; margin-top: 10px; }

.sequence-list {
  display: grid;
  gap: 8px;
}

.move-card {
  display: grid;
  grid-template-columns: auto 1fr auto;
  gap: 10px;
  align-items: center;
  padding: 10px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fbfcfd;
  animation: slideIn 180ms ease-out;
}
.move-card .idx { color: var(--muted); font-weight: 800; }
.move-card .token { font-size: 20px; font-weight: 900; }
.move-card.part-1 .token { color: var(--blue); }
.move-card.part-2 .token { color: var(--violet); }
.move-card small { color: var(--muted); }
.sequence-more { margin-top: 2px; }

.occupancy-chart {
  display: flex;
  align-items: end;
  gap: 5px;
  min-height: 42px;
  margin-top: 10px;
  overflow-x: auto;
  padding-bottom: 2px;
}

.tray-note {
  margin-top: 10px;
  padding: 10px 12px;
  border-left: 4px solid var(--green);
  border-radius: 8px;
  background: var(--green-soft);
  color: #175b37;
  font-size: 14px;
  line-height: 1.35;
  font-weight: 800;
}

.layout-summary {
  margin-top: 10px;
  color: var(--muted);
  font-size: 14px;
  font-weight: 800;
  line-height: 1.35;
}

.modal {
  position: fixed;
  inset: 0;
  z-index: 50;
  display: grid;
  place-items: center;
  padding: 18px;
  background: rgba(0, 0, 0, 0.52);
}

.modal.hidden {
  display: none;
}

.modal-card {
  width: min(100%, 420px);
  padding: 16px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--panel);
  box-shadow: var(--shadow);
}

.modal-card h2 {
  margin-bottom: 12px;
}

body.is-layout-open {
  overflow: hidden;
}

body.is-layout-open .app-shell,
body.is-layout-open .bottom-actions {
  display: none;
}

.layout-editor {
  display: flex;
  flex-direction: column;
  gap: 8px;
  height: 100vh;
  height: 100dvh;
  min-height: 0;
  width: min(100%, 760px);
  margin: 0 auto;
  padding: calc(8px + env(safe-area-inset-top)) 10px calc(8px + env(safe-area-inset-bottom));
  background: var(--bg);
  color: var(--text);
  overflow: hidden;
}

.layout-editor.hidden {
  display: none;
}

.layout-editor-head {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 10px;
}

.layout-editor-head p {
  margin-top: 2px;
  color: var(--muted);
  font-weight: 800;
}

.layout-editor-head button {
  flex: 0 0 auto;
  background: var(--red-soft);
  color: var(--red);
}

.layout-toolbar {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 7px;
}

.layout-toolbar-single {
  grid-template-columns: 1fr;
}

.layout-toolbar button {
  min-height: 42px;
  padding: 8px 10px;
}

#prevLayer,
#nextLayer {
  background: var(--blue-soft);
  color: #164aa7;
}

#numberLayout {
  background: var(--green);
  color: #fff;
}

#clearLayer {
  background: #eb0000;
  color: #fff;
}

#clearLayoutAll {
  background: #a30000;
  color: #fff;
  font-size: 14px;
}

.layout-hint {
  padding: 8px 10px;
  border-radius: 8px;
  background: var(--yellow-soft);
  color: #5d4700;
  font-size: 13px;
  font-weight: 800;
  line-height: 1.35;
}

.layout-grid-settings {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 7px;
}

.layout-grid-settings label {
  margin: 0;
  display: grid;
  grid-template-columns: auto minmax(0, 1fr);
  align-items: center;
  gap: 8px;
  padding: 7px 9px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--panel);
  font-size: 13px;
  font-weight: 900;
}

.layout-grid-settings input {
  min-width: 0;
  padding: 6px 8px;
  text-align: center;
  font-weight: 900;
}

.layout-board-wrap {
  flex: 1 1 auto;
  min-height: 0;
  display: block;
  overflow: auto;
  padding: 8px 0 18px;
  scroll-padding-top: 8px;
  -webkit-overflow-scrolling: touch;
}

.numbering-prompt {
  flex: 0 0 auto;
  min-height: 58px;
  padding: 9px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--panel);
  color: var(--muted);
  text-align: center;
  font-weight: 900;
}

.numbering-prompt.is-active {
  display: grid;
  grid-template-columns: 1fr;
  align-items: center;
  justify-items: center;
  gap: 7px;
  background: var(--blue-soft);
  color: #164aa7;
}

.numbering-prompt strong {
  font-size: 42px;
  line-height: 1;
  color: var(--text-on-light);
}

.numbering-top,
.numbering-bottom {
  font-size: 13px;
  line-height: 1.25;
}

.numbering-prompt button {
  width: 100%;
  margin-top: 4px;
  background: var(--red-soft);
  color: var(--red);
}

.layout-board {
  --grid-width: 8;
  --grid-height: 6;
  --half-x: 6.25%;
  --half-y: 8.3333%;
  --cell-x: 12.5%;
  --cell-y: 16.6667%;
  position: relative;
  width: 100%;
  max-width: 720px;
  aspect-ratio: var(--grid-width) / var(--grid-height);
  min-width: 0;
  margin: 0 auto;
  border: 1px solid #626943;
  border-radius: 4px;
  background-color: #20313a;
  background-image:
    linear-gradient(to right, rgba(229, 216, 119, 0.26) 2px, transparent 2px),
    linear-gradient(to bottom, rgba(229, 216, 119, 0.26) 2px, transparent 2px),
    linear-gradient(to right, rgba(229, 216, 119, 0.44) 2px, transparent 2px),
    linear-gradient(to bottom, rgba(229, 216, 119, 0.44) 2px, transparent 2px);
  background-size:
    var(--half-x) var(--half-y),
    var(--half-x) var(--half-y),
    var(--cell-x) var(--cell-y),
    var(--cell-x) var(--cell-y);
  touch-action: pan-y;
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
}

.layout-tile {
  position: absolute;
  width: var(--cell-x);
  height: var(--cell-y);
  min-height: 0;
  padding: 0;
  border: 2px solid #12120d;
  border-radius: 3px;
  background: var(--tile-bg, #f6f8fa);
  color: var(--tile-color, #17212b);
  font-size: 14px;
  font-weight: 950;
  box-shadow: inset 0 0 0 1px rgba(255, 255, 255, 0.2);
  touch-action: none;
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
}

.layout-tile.is-under-layer {
  box-shadow: inset 0 0 0 1px rgba(255, 255, 255, 0.08);
}

.layout-tile.is-active-layer {
  outline: 2px solid #f2cf43;
  outline-offset: -2px;
}

.layout-tile.is-numbered {
  font-size: 20px;
}

.layout-tile.is-dragging {
  z-index: 20;
  opacity: 0.92;
  box-shadow: 0 10px 24px rgba(0, 0, 0, 0.35);
}
.bar {
  min-width: 20px;
  height: calc(10px + var(--level) * 9px);
  border-radius: 5px 5px 2px 2px;
  background: var(--green);
}
.bar.level-2 { background: var(--yellow); }
.bar.level-3 { background: var(--orange); }
.bar.level-4 { background: var(--red); }

.reverse-box {
  min-height: 44px;
  padding: 12px;
  border-radius: 8px;
  background: #f1f4f7;
  line-height: 1.5;
  word-break: break-word;
}

.stats-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
  margin: 0;
}
.stats-grid div {
  padding: 10px;
  border-radius: 8px;
  background: #f6f8fa;
}
.stats-grid dt { color: var(--muted); font-size: 12px; font-weight: 800; }
.stats-grid dd { margin: 3px 0 0; font-size: 20px; font-weight: 900; }

.bottom-actions {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 10;
  display: grid;
  grid-template-columns: 1fr 1fr 1fr 1.35fr;
  gap: 8px;
  padding: 10px 12px calc(10px + env(safe-area-inset-bottom));
  background: rgba(255, 255, 255, 0.94);
  border-top: 1px solid var(--line);
  backdrop-filter: blur(12px);
}

.bottom-actions button {
  padding-left: 8px;
  padding-right: 8px;
  font-size: 13px;
}

.hidden { display: none; }

body.is-separate-open {
  overflow: hidden;
}

.separate-view {
  position: fixed;
  inset: 0;
  z-index: 30;
  display: flex;
  flex-direction: column;
  min-height: 100vh;
  padding: calc(16px + env(safe-area-inset-top)) 16px calc(16px + env(safe-area-inset-bottom));
  background: var(--bg);
  color: var(--text);
}

.separate-view.hidden {
  display: none;
}

.separate-header {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 14px;
}

.separate-header button {
  flex: 0 0 auto;
  background: var(--red-soft);
  color: var(--red);
}

.separate-card {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 18px;
  margin: 18px 0;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--panel);
  box-shadow: var(--shadow);
  text-align: center;
}

.separate-token {
  font-size: 76px;
  line-height: 1;
  font-weight: 950;
  color: var(--blue);
}

.pair-badge {
  min-width: 164px;
  padding: 12px 18px;
  border-radius: 8px;
  color: #fff;
  font-size: 22px;
  line-height: 1;
  font-weight: 950;
}

.pair-badge.has-pair {
  background: var(--green);
}

.pair-badge.no-pair {
  background: var(--red);
}

.separate-step {
  font-size: 28px;
  font-weight: 900;
  color: var(--muted);
}

.separate-progress {
  margin-bottom: 10px;
  text-align: center;
  color: var(--muted);
  font-weight: 800;
}

.separate-actions {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
}

.separate-actions button {
  min-height: 56px;
  background: var(--text);
  color: var(--panel);
  font-size: 18px;
}

.separate-actions button:disabled {
  opacity: 0.45;
}

@keyframes pop {
  from { opacity: 0; transform: scale(0.96); }
  to { opacity: 1; transform: scale(1); }
}
@keyframes slideIn {
  from { opacity: 0; transform: translateY(8px); }
  to { opacity: 1; transform: translateY(0); }
}
@keyframes shake {
  0%, 100% { transform: translateX(0); }
  25% { transform: translateX(-5px); }
  75% { transform: translateX(5px); }
}

@media (min-width: 720px) {
  .app-shell {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 12px;
  }
  .topbar, .tray-panel { grid-column: 1 / -1; }
  .panel { margin: 0; }
}

@media (prefers-color-scheme: dark) {
  :root {
    color-scheme: dark;
    --bg: #121417;
    --panel: #1b2026;
    --text: #eef2f7;
    --text-on-light: #17212b;
    --muted: #a8b3bf;
    --line: #303943;
    --shadow: none;
  }
  input, textarea, select, .tray-cell, .move-card { background: #202832; color: var(--text); }
  .bottom-actions { background: rgba(27, 32, 38, 0.94); }
  button,
  .ghost,
  .save-top,
  .counter,
  .stats-grid div,
  .reverse-box {
    color: var(--text-on-light);
  }
  .danger-zone {
    color: #ffffff;
  }
  .tab.is-active,
  .primary,
  .secondary {
    color: #fff;
  }
  .tile {
    color: var(--text-on-light);
  }
  .next-card-box,
  .tile-action {
    background: #f6f8fa;
    color: var(--text-on-light);
  }
  .tile span,
  .move-card small {
    color: #5f6b76;
  }
  .separate-actions button {
    color: #17212b;
  }
  .tray-note {
    color: var(--text-on-light);
  }
  .layout-hint {
    color: #5d4700;
  }
  .layout-editor-head button {
    color: var(--red);
  }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 0.001ms !important;
    scroll-behavior: auto !important;
  }
}
