/* Rubik (SIL Open Font License, fonts/OFL.txt), variable weight, served from this repo. */
@font-face {
  font-family: "Rubik";
  font-style: normal;
  font-weight: 300 900;
  font-display: swap;
  src: url(fonts/rubik-cyrillic.woff2) format("woff2");
  unicode-range: U+0301, U+0400-045F, U+0490-0491, U+04B0-04B1, U+2116;
}
@font-face {
  font-family: "Rubik";
  font-style: normal;
  font-weight: 300 900;
  font-display: swap;
  src: url(fonts/rubik-latin.woff2) format("woff2");
  unicode-range: U+0000-00FF, U+0131, U+0152-0153, U+02BB-02BC, U+02C6, U+02DA, U+02DC, U+0304, U+0308, U+0329, U+2000-206F, U+20AC, U+2122, U+2191, U+2193, U+2212, U+2215, U+FEFF, U+FFFD;
}
@font-face {
  font-family: "Rubik";
  font-style: normal;
  font-weight: 300 900;
  font-display: swap;
  src: url(fonts/rubik-latin-ext.woff2) format("woff2");
  unicode-range: U+0100-02BA, U+02BD-02C5, U+02C7-02CC, U+02CE-02D7, U+02DD-02FF, U+0304, U+0308, U+0329, U+1D00-1DBF, U+1E00-1E9F, U+1EF2-1EFF, U+2020, U+20A0-20AB, U+20AD-20C4, U+2113, U+2C60-2C7F, U+A720-A7FF;
}

:root {
  --bg: #0f1218;
  --panel: #171b24;
  --panel-2: #1f2430;
  --text: #e9e6df;
  --muted: #8d94a3;
  --accent: #ffd84d;
  --board: #1a1f29;
  --board-dot: #2a3140;
  --border: #2a3040;
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  height: 100dvh;
  overscroll-behavior: none;
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
  background: radial-gradient(1200px 600px at 50% -10%, #1d2633 0%, var(--bg) 60%) fixed, var(--bg);
  color: var(--text);
  font-family: "Rubik", "Segoe UI", system-ui, sans-serif;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  -webkit-tap-highlight-color: transparent;
}

.top {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: max(12px, env(safe-area-inset-top)) max(16px, env(safe-area-inset-right)) 12px max(16px, env(safe-area-inset-left));
}
.title { display: flex; align-items: center; gap: 12px; min-width: 0; }
.logo { font-size: 30px; }
.level-num { color: var(--muted); font-size: 13px; letter-spacing: .04em; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.title > div { min-width: 0; }
h1 { margin: 0; font-size: 20px; font-weight: 700; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.actions { display: flex; gap: 8px; flex-shrink: 0; }

.btn {
  font: inherit;
  font-weight: 500;
  font-size: 14px;
  color: var(--text);
  background: var(--panel-2);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 8px 12px;
  cursor: pointer;
  transition: background .15s, transform .05s;
}
.btn:hover { background: #283042; }
.btn:active { transform: translateY(1px); }
.btn:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
.btn.primary { background: var(--accent); color: #1a1608; border-color: transparent; font-weight: 700; }
.btn.primary:hover { background: #ffe27a; }

/* Desktop: board | side panel. Touch screens add the d-pad under the panel;
   phones in portrait stack board / panel / d-pad; phones in landscape put panel + d-pad on the right. */
.layout {
  flex: 1;
  min-height: 0;
  display: grid;
  grid-template-columns: 1fr 260px;
  grid-template-rows: minmax(0, 1fr);
  grid-template-areas: "stage side";
  gap: 16px;
  padding: 0 max(16px, env(safe-area-inset-right)) max(16px, env(safe-area-inset-bottom)) max(16px, env(safe-area-inset-left));
}
.stage { grid-area: stage; position: relative; min-height: 0; min-width: 0; }
.side { grid-area: side; min-height: 0; }
#board { display: block; touch-action: none; }

.side { display: flex; flex-direction: column; gap: 12px; overflow-y: auto; }
.card {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 12px 14px;
}
.card h3 { margin: 0 0 8px; font-size: 12px; text-transform: uppercase; letter-spacing: .08em; color: var(--muted); font-weight: 700; }
.hint { margin: 0; line-height: 1.45; }
.keys p { margin: 4px 0; font-size: 14px; color: #c9cbd2; }
kbd {
  font: inherit; font-size: 12px; font-weight: 700;
  background: var(--panel-2); border: 1px solid var(--border); border-bottom-width: 2px;
  border-radius: 6px; padding: 1px 6px;
}

.rules { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 6px; }
.rules li { display: flex; flex-wrap: wrap; gap: 4px; }
.rules .muted { color: var(--muted); font-style: italic; }
.chip {
  font-size: 11px; font-weight: 800; letter-spacing: .02em;
  padding: 3px 6px; border-radius: 6px;
  border: 1.5px solid var(--c); color: var(--c); background: #12151c;
}
.chip-op, .chip-and { border-color: rgba(255,255,255,.4); color: #fff; }
.chip-prop { background: var(--c); color: #17141a; }

.banner {
  position: absolute;
  left: 50%; bottom: 24px; transform: translateX(-50%);
  width: min(440px, calc(100% - 32px));
  background: rgba(23, 27, 36, 0.96);
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 16px 20px;
  text-align: center;
  box-shadow: 0 20px 50px rgba(0,0,0,.5);
  animation: pop .25s ease-out;
}
.banner[data-kind="won"] { border-color: var(--accent); }
.banner h2 { margin: 0 0 4px; font-size: 22px; }
.banner[data-kind="won"] h2 { color: var(--accent); }
.banner p { margin: 0 0 12px; color: #c9cbd2; }
.banner .row { display: flex; justify-content: center; gap: 8px; flex-wrap: wrap; }
.banner small { display: block; margin-top: 8px; color: var(--muted); }
@keyframes pop { from { opacity: 0; transform: translate(-50%, 10px); } }

.menu {
  position: fixed; inset: 0;
  padding: max(16px, env(safe-area-inset-top)) max(16px, env(safe-area-inset-right)) max(16px, env(safe-area-inset-bottom)) max(16px, env(safe-area-inset-left));
  background: rgba(10, 12, 17, 0.88);
  backdrop-filter: blur(6px);
  display: grid; place-items: center;
  z-index: 10;
}
.menu[hidden] { display: none; }
.menu-inner { width: min(760px, 100%); text-align: center; max-height: 100%; overflow-y: auto; padding: 4px; }
.menu h2 { font-size: 38px; margin: 0 0 6px; font-weight: 800; }
.lead { color: #c9cbd2; margin: 0 0 22px; }
.level-grid { margin-bottom: 18px; text-align: left; }
.chapter-head {
  display: flex; justify-content: space-between; align-items: baseline;
  margin: 22px 2px 10px; font-size: 18px; font-weight: 700;
}
.chapter-head:first-child { margin-top: 0; }
.chapter-head small { color: var(--muted); font-weight: 500; font-size: 14px; }
.level-row {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(140px, 1fr));
  gap: 10px;
}
.level-card {
  font: inherit; color: var(--text);
  background: var(--panel); border: 1px solid var(--border); border-radius: 14px;
  padding: 14px 10px; cursor: pointer; display: flex; flex-direction: column; align-items: center; gap: 6px;
  transition: transform .1s, border-color .15s;
}
.level-card:hover:not(:disabled) { transform: translateY(-2px); border-color: #3d475c; }
.level-card:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
.level-card .n { font-size: 26px; font-weight: 800; }
.level-card .t { font-size: 13px; color: #c9cbd2; }
.level-card.done { border-color: #3f6e4a; }
.level-card.done .n::after { content: ' ✓'; color: #5fbf77; font-size: 18px; }
.level-card.current { border-color: var(--accent); }
.level-card:disabled { opacity: .45; cursor: not-allowed; }

.menu-actions { display: flex; gap: 8px; justify-content: center; flex-wrap: wrap; }

.dpad { grid-area: dpad; display: none; }
.dpad { align-items: center; justify-content: center; gap: 22px; }
.cross {
  display: grid;
  grid-template-columns: repeat(3, 64px);
  grid-template-rows: repeat(3, 52px);
  gap: 8px;
  touch-action: none;        /* the cross is one touch pad (see main.js); sliding must not scroll */
  user-select: none; -webkit-user-select: none; -webkit-touch-callout: none;
}
.cross [data-dir="up"]    { grid-area: 1 / 2; }
.cross [data-dir="left"]  { grid-area: 2 / 1; }
.cross [data-dir="wait"]  { grid-area: 2 / 2; font-size: 13px; }
.cross [data-dir="right"] { grid-area: 2 / 3; }
.cross [data-dir="down"]  { grid-area: 3 / 2; }
.dpad button {
  font: inherit; font-size: 20px; color: var(--text);
  background: var(--panel-2); border: 1px solid var(--border); border-radius: 14px;
  touch-action: manipulation;
  cursor: pointer;
}
.dpad button:active, .dpad button.held { background: #2b3446; transform: translateY(1px); }
/* Undo / rules sit apart from the cross and look secondary, so they are hard to hit by accident. */
.dpad .pad-side {
  width: 54px; height: 64px; align-self: end;
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px;
  background: transparent; color: var(--muted); border-style: dashed; font-size: 20px;
}
.pad-side small { font-size: 10px; letter-spacing: .02em; }

/* Any touch screen: show the d-pad under the side panel. */
@media (pointer: coarse) {
  .layout { grid-template-rows: minmax(0, 1fr) auto; grid-template-areas: "stage side" "stage dpad"; }
  .dpad { display: flex; }
  .dpad .pad-side { display: none; }   /* the header keeps undo / restart in this layout */
}

/* Sheet bits (only used on phones; hidden on desktop). */
.sheet-head, .hdr-rules, .sheet-backdrop, .rotate-tip { display: none; }
.rotate-tip {
  position: absolute; left: 50%; bottom: 4px; transform: translateX(-50%);
  background: rgba(23, 27, 36, 0.92); border: 1px solid var(--border); border-radius: 999px;
  padding: 5px 12px; font-size: 12px; color: var(--muted); white-space: nowrap; pointer-events: none;
}

/* Phones (portrait and landscape): the board gets the screen; rules and hint open in a sheet. */
@media (max-width: 820px) and (orientation: portrait), (max-height: 540px) and (orientation: landscape) {
  .side {
    position: fixed; z-index: 8; left: 0; right: 0; bottom: 0;
    max-height: 72dvh; overflow-y: auto; overscroll-behavior: contain;
    display: flex; flex-direction: column; gap: 10px;
    background: var(--bg); border-top: 1px solid var(--border); border-radius: 18px 18px 0 0;
    padding: 14px max(14px, env(safe-area-inset-right)) max(16px, env(safe-area-inset-bottom)) max(14px, env(safe-area-inset-left));
    box-shadow: 0 -18px 40px rgba(0,0,0,.45);
    transform: translateY(105%); visibility: hidden;
    transition: transform .22s ease-out, visibility 0s .22s;
  }
  body.rules-open .side { transform: none; visibility: visible; transition: transform .22s ease-out; }
  body.rules-open .sheet-backdrop { display: block; position: fixed; inset: 0; z-index: 7; background: rgba(5,7,10,.5); }
  .sheet-head { display: flex; align-items: center; justify-content: space-between; font-size: 15px; }
  .sheet-head .btn { padding: 6px 12px; }
  .side .keys { display: none; }
  .hint { font-size: 15px; line-height: 1.4; }
  .actions .lbl { display: none; }
  .hdr-play { display: none; }
  .dpad { display: flex; }
  .rotate-tip:not([hidden]) { display: block; }
}
@media (prefers-reduced-motion: reduce) { .side { transition: none !important; } }

/* Phone / narrow window, portrait: header, board, controls. */
@media (max-width: 820px) and (orientation: portrait) {
  .layout {
    grid-template-columns: minmax(0, 1fr);
    grid-template-rows: minmax(0, 1fr) auto;
    grid-template-areas: "stage" "dpad";
    gap: 8px;
    padding-left: max(4px, env(safe-area-inset-left));
    padding-right: max(4px, env(safe-area-inset-right));
  }
  .dpad .pad-side { display: flex; }
  .dpad { gap: 14px; padding-bottom: max(4px, env(safe-area-inset-bottom)); }
  .cross { grid-template-columns: repeat(3, 72px); grid-template-rows: repeat(3, 60px); gap: 6px; }
  .dpad button { font-size: 22px; }
  .top { padding-bottom: 6px; }
  .logo { display: none; }
  h1 { font-size: 17px; }
  .level-num { font-size: 12px; }
}

/* Phone in landscape: board on the left, cross on the right, buttons in the header. */
@media (max-height: 540px) and (orientation: landscape) {
  .top { padding-top: max(6px, env(safe-area-inset-top)); padding-bottom: 4px; }
  .logo { display: none; }
  h1 { font-size: 16px; }
  .level-num { font-size: 11px; }
  .btn { padding: 6px 10px; }
  .hdr-play, .hdr-rules { display: inline-block; }
  .layout {
    grid-template-columns: minmax(0, 1fr) auto;
    grid-template-rows: minmax(0, 1fr);
    grid-template-areas: "stage dpad";
    gap: 8px;
    padding-bottom: max(6px, env(safe-area-inset-bottom));
    padding-left: max(6px, env(safe-area-inset-left));
  }
  .dpad .pad-side { display: none; }
  .cross { grid-template-columns: repeat(3, 58px); grid-template-rows: repeat(3, 52px); gap: 4px; }
  .side { max-height: 85dvh; }
  .banner { bottom: 8px; padding: 10px 14px; }
  .banner h2 { font-size: 18px; }
  .banner p { margin-bottom: 8px; }
}

@media (max-width: 380px) and (orientation: portrait) {
  .dpad { gap: 10px; }
  .cross { grid-template-columns: repeat(3, 68px); grid-template-rows: repeat(3, 56px); gap: 5px; }
  .dpad .pad-side { width: 44px; }
  .actions { gap: 6px; }
  .btn { padding: 7px 10px; }
}

@media (pointer: coarse), (max-width: 820px) and (orientation: portrait) {
  .banner small { display: none; }
}
