:root {
  --bg: #efe4cc;
  --ink: #2b2118;
  --muted: #6b5a47;
  --accent: #8b2c1d;
  --paper: #fbf7ec;
  --line: #c9b78f;
  --btn: #f6efdc;
  --btn-hover: #fffaf0;
  --shadow: 0 1px 2px rgb(60 40 10 / 20%), 0 8px 24px rgb(60 40 10 / 18%);

  /* Tile size: fit the width (6 tiles + 2 gutters) and the height (8 tiles + 2 gutters).
     --gutter-max: widest gutter; --side: width taken by the side column (wide layout);
     --chrome: height taken by everything but the 8 tile rows (page above the map, gutters, padding). */
  --gutter-max: 2.6rem;
  --side: 0rem;
  --chrome: 23rem;
  --gutter: clamp(1.5rem, calc(var(--tile) * 0.42), var(--gutter-max));
  --tile: clamp(40px, min(calc((100vw - 2rem - var(--side) - 2 * var(--gutter-max)) / 6), calc((100dvh - var(--chrome)) / 8)), 150px);
  --t: calc(var(--tile) * 0.85);
  color-scheme: light dark;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #1e1914;
    --ink: #eadfc8;
    --muted: #a8977e;
    --accent: #e08a6f;
    --line: #5a4a38;
    --btn: #2d251d;
    --btn-hover: #3a3025;
    --shadow: 0 1px 2px rgb(0 0 0 / 50%), 0 8px 24px rgb(0 0 0 / 45%);
  }
}

* { box-sizing: border-box; }

body {
  margin: 0;
  padding: 1rem;
  background: var(--bg);
  color: var(--ink);
  font: 1rem/1.55 system-ui, -apple-system, "Segoe UI", sans-serif;
}

a { color: var(--accent); text-underline-offset: 0.15em; }

h1 {
  margin: 0;
  font: 700 clamp(1.8rem, 5vw, 2.6rem)/1.1 Georgia, "Iowan Old Style", "Palatino Linotype", serif;
  letter-spacing: 0.01em;
}

.page-head, .toolbar, .status, main, .page-foot { max-width: 60rem; margin-inline: auto; }
.lede { margin: 0.25rem 0 0.75rem; color: var(--muted); font-size: 1.1rem; }

details { margin-bottom: 1rem; }
summary { cursor: pointer; color: var(--accent); font-weight: 600; width: max-content; }
summary:focus-visible { outline: 2px solid var(--accent); outline-offset: 3px; border-radius: 2px; }
details[open] { padding: 0.25rem 1rem; background: var(--paper); color: #2b2118; border: 1px solid var(--line); border-radius: 6px; }
details[open] a { color: #8b2c1d; }
details[open] summary { margin-top: 0.5rem; }
details p { max-width: 42rem; }

kbd {
  font: 0.85em ui-monospace, monospace;
  padding: 0.05em 0.4em;
  border: 1px solid var(--line);
  border-bottom-width: 2px;
  border-radius: 4px;
}

/* ---- buttons ---- */
button {
  font: inherit;
  color: var(--ink);
  background: var(--btn);
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 0.35rem 0.8rem;
  cursor: pointer;
  touch-action: manipulation;
}
/* Touch screens fake :hover after a tap and leave it stuck, so only real pointers get it. */
@media (hover: hover) {
  button:hover:not(:disabled) { background: var(--btn-hover); border-color: var(--accent); }
}
button:active:not(:disabled) { transform: translateY(1px); }
button:disabled { opacity: 0.45; cursor: default; }
button[aria-pressed="true"] { background: var(--accent); border-color: var(--accent); color: var(--paper); }
button:focus-visible { outline: 3px solid var(--accent); outline-offset: 2px; }

.toolbar { display: flex; flex-wrap: wrap; gap: 0.4rem; align-items: center; margin-bottom: 0.25rem; }
.toolbar button { flex: none; white-space: nowrap; }
.sep { width: 1px; align-self: stretch; margin-inline: 0.3rem; background: var(--line); }
.picker {
  max-width: 60rem; margin: 0.4rem auto 0;
  display: flex; flex-wrap: wrap; gap: 0.25rem 0.8rem; align-items: center;
}
.set-group { color: var(--muted); }
.set-group label { color: var(--ink); white-space: nowrap; cursor: pointer; }
.sets input { accent-color: var(--accent); }
/* Both labels share one cell so the button keeps the width of the longer one. */
.deal { display: inline-grid; }
.deal > span { grid-area: 1 / 1; }
.deal:disabled .on, .deal:not(:disabled) .off { visibility: hidden; }
.status { min-height: 1.6em; margin-block: 0.25rem 0.5rem; color: var(--muted); font-size: 0.9rem; }

/* ---- flat map ---- */
main { display: flex; justify-content: center; }

.net {
  display: grid;
  grid-template-columns: var(--gutter) repeat(6, var(--tile)) var(--gutter);
  grid-template-rows: var(--gutter) repeat(8, var(--tile)) var(--gutter);
  width: max-content;
  background: var(--paper);
  border-radius: 8px;
  box-shadow: var(--shadow);
  touch-action: manipulation;
}
.net[hidden], .cube-view[hidden] { display: none; }

.geomorph {
  display: block;
  width: 100%;
  height: 100%;
  background: #fff;
  outline: 1px solid rgb(0 0 0 / 22%);
  outline-offset: -1px;
  user-select: none;
  -webkit-touch-callout: none;
}
#map .geomorph:not(.blank) { cursor: pointer; }
.geomorph.blank { background: repeating-linear-gradient(45deg, #fff 0 8px, #f1ece0 8px 16px); }
/* Same outline for a turn button's tiles and for the single tile under the pointer. */
.geomorph.hl { outline: 4px solid var(--accent); outline-offset: -4px; position: relative; z-index: 1; }
@media (hover: hover) {
  #map .geomorph:not(.blank):hover { outline: 4px solid var(--accent); outline-offset: -4px; position: relative; z-index: 1; }
}

/* Face borders and (optional) face numbers sit above the tiles but never catch the pointer. */
.face { position: relative; z-index: 1; pointer-events: none; box-shadow: inset 0 0 0 2px rgb(0 0 0 / 60%); display: grid; place-items: center; }
.face::after, .cube-face::after {
  content: attr(data-face);
  display: none;
  font: 700 calc(var(--tile) * 1.1)/1 Georgia, serif;
  color: rgb(139 44 29 / 45%);
  pointer-events: none;
}
.show-faces .face::after, .show-faces .cube-face::after { display: block; }
.cube-face { display: grid; place-items: center; }
.cube-face .geomorph { grid-area: 1 / 1; }

/* ---- arrows ---- */
.arrow { padding: 0; margin: 2px; line-height: 1; border-radius: 4px; display: grid; place-items: center; }
.arrow span { display: flex; align-items: center; justify-content: center; gap: 0.2em; font-size: 0.8rem; }
.arrow b { font-weight: 700; }
.arrow[data-move$="left"] span, .arrow[data-move$="right"] span { flex-direction: column; }

/* ---- 3D view ---- */
.cube-view { display: grid; justify-items: center; gap: 1rem; width: 100%; }
.scene {
  --tile: clamp(40px, calc((100vw - 2rem) / 4.6), 140px);
  --t: calc(var(--tile) * 0.85);
  width: calc(var(--tile) * 4.6);
  height: calc(var(--tile) * 4.2);
  display: grid;
  place-items: center;
  perspective: calc(var(--tile) * 14);
  touch-action: none;
  cursor: grab;
}
.scene:active { cursor: grabbing; }
.cube3d { position: relative; width: calc(var(--t) * 2); height: calc(var(--t) * 2); transform-style: preserve-3d; }
.cube-face {
  position: absolute;
  inset: 0;
  grid-template-columns: 1fr 1fr;
  grid-template-rows: 1fr 1fr;
  backface-visibility: hidden;
  background: #fff;
  box-shadow: inset 0 0 0 2px rgb(0 0 0 / 60%);
}
.cube-face::after { position: absolute; inset: 0; place-self: center; text-align: center; font-size: calc(var(--t) * 1.4); }
.cube-face .geomorph { grid-area: auto; }
.cube-face:has(.hl) { z-index: 1; }

.moves { display: grid; grid-template-columns: repeat(6, auto); gap: 0.3rem; justify-content: center; }
.moves button { padding: 0.3rem 0.6rem; font-size: 0.9rem; }

.page-foot { margin-top: 2rem; color: var(--muted); font-size: 0.9rem; }

@media (prefers-reduced-motion: reduce) {
  button:active:not(:disabled) { transform: none; }
}

/* ---- print: just the flat map, big and black on white ---- */
@page { margin: 0.4in; }
@media print {
  :root { --tile: 1.2in; --gutter: 0; }
  body { background: #fff; padding: 0; }
  .page-head, .toolbar, .picker, .status, .page-foot > :not(#tile-credit), .cube-view, .arrow, noscript { display: none !important; }
  .page-foot { margin-top: 0.15in; text-align: center; color: #000; font-size: 9pt; }
  .net, .net[hidden] { display: grid !important; box-shadow: none; background: #fff; margin-inline: auto; }
  .geomorph.hl { outline: 1px solid rgb(0 0 0 / 22%); outline-offset: -1px; }
  .show-faces .face::after { display: none; }
}

/* ---- touch screens: finger-sized targets (about 44px) ---- */
@media screen and (pointer: coarse) {
  :root { --gutter-max: 3rem; --gutter: var(--gutter-max); --chrome: 31rem; }
  .arrow[data-move$="up"], .arrow[data-move$="down"] { min-height: 2.75rem; }
  .arrow[data-move$="left"], .arrow[data-move$="right"] { min-width: 2.75rem; }
  button { padding: 0.55rem 0.9rem; }
  .moves button { padding: 0.55rem 0.7rem; }
  .picker { gap: 0.5rem 1rem; }
  .set-group { line-height: 2.2; }
  .sets input { width: 1.25rem; height: 1.25rem; vertical-align: -0.25em; }
}

/* ---- wide screens: controls in a side column so the map gets the full height ---- */
@media screen and (orientation: landscape) and (min-width: 56rem) {
  :root {
    --side: 22rem; /* side column + gap */
    --chrome: calc(2rem + 2 * var(--gutter-max));
  }
  body {
    display: grid;
    grid-template-columns: 20.5rem 1fr;
    grid-template-rows: auto auto auto auto 1fr;
    column-gap: 1.5rem;
    align-items: start;
  }
  .page-head, .toolbar, .picker, .status, .page-foot { grid-column: 1; max-width: none; margin-inline: 0; }
  main { grid-column: 2; grid-row: 1 / -1; max-width: none; margin-inline: 0; }
  .page-foot { grid-row: 5; margin-top: 1rem; }
  .lede { font-size: 1rem; }
  .sep { display: none; }
  .set-group { width: 100%; }
}

/* ---- phones ---- */
@media screen and (max-width: 40rem) {
  .sep { display: none; }
  .moves .kind { display: none; }
}
