:root {
  --cell: 56px;
  --gap: 4px;

  --bg: #f3efe6;
  --panel: #ffffffcc;
  --border: #d6cfbf;
  --text: #111111;

  --tile-radius: 18px;
  --tile-border: 1px;

  --grid-pad: 12px;
  --grid-border: 1px;

  /* OUTER board width: 6 cells + 5 gaps + padding*2 + border*2 */
  --board-outer: calc(
    (6 * var(--cell)) +
    (5 * var(--gap)) +
    (2 * var(--grid-pad)) +
    (2 * var(--grid-border))
  );

  --board-inner: calc(
    (6 * var(--cell)) + (5 * var(--gap))
  );
}

/* -------------------------------------------------
   Global reset-ish
-------------------------------------------------- */

* { box-sizing: border-box; }

[hidden] { display: none !important; }

html, body {
  margin: 0;
  width: 100%;
  min-height: 100%;
  overflow-x: hidden;
  background: var(--bg);
}

body {
  font-family: "Lora", serif;
  color: var(--text);

  /* Safe-area padding for notched iPhones */
  padding-left: env(safe-area-inset-left);
  padding-right: env(safe-area-inset-right);

  /* Prevent selection/zoom weirdness */
  -webkit-user-select: none;
  user-select: none;
  -webkit-tap-highlight-color: transparent;
}

button, input, select, textarea {
  font-family: inherit;
}

/* A11y: keyboard focus visibility */
.btn:focus-visible {
  outline: 3px solid rgba(17, 17, 17, 0.6);
  outline-offset: 3px;
}

/* Utility: screen reader only */
.srOnly {
  position: absolute !important;
  width: 1px !important;
  height: 1px !important;
  padding: 0 !important;
  margin: -1px !important;
  overflow: hidden !important;
  clip: rect(0, 0, 0, 0) !important;
  white-space: nowrap !important;
  border: 0 !important;
}

/* -------------------------------------------------
   App layout
-------------------------------------------------- */

.app {
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  align-items: center;
}

/* Screens */
.screen {
  display: none;
  width: 100%;
  max-width: 520px;
  padding:
    clamp(12px, 3vh, 24px)
    clamp(12px, 4vw, 24px);

  /* prevent children from forcing overflow on small screens */
  min-width: 0;
}

.screen--active {
  display: block;
  width: 100%;
  max-width: 520px;
}

/* Start screen layout */
#startScreen.screen--active {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
}

.startStack {
  width: 100%;
  max-width: 320px;
  display: flex;
  flex-direction: column;
  gap: 18px;
  margin-top: 0;
}

/* -------------------------------------------------
   Buttons
-------------------------------------------------- */

.btn {
  width: 100%;
  max-width: 140px;
  padding: 12px 20px;
  font-size: 16px;
  border-radius: 12px;
  border: 1px solid var(--border);
  background: #fff;
  color: var(--text);
  cursor: pointer;
  text-align: center;

  margin: 0 auto;

  font-weight: 800;
  letter-spacing: 0.25em;

  /* optical centering for letter-spacing */
  padding-left: calc(20px + 0.125em);
  padding-right: calc(20px - 0.125em);

  display: inline-flex;
  align-items: center;
  justify-content: center;
}

#playBtn {
  margin-top: 24px;
}

.btn:disabled,
.btn[disabled] {
  opacity: 0.45;
  cursor: default;
}

/* Only apply hover styling to interactive buttons */
.btn:not(:disabled):hover {
  background: #f7f4ec;
}

.btnLabel {
  letter-spacing: 0.25em;
  display: inline-block;
  margin-right: -0.25em; /* cancels trailing letter-spacing */
}

/* Compact buttons for the control row */
.btn--small {
  max-width: none;
  width: auto;
  padding: 10px 14px;
  font-size: 13px;
}

/* Hint counter inside button */
.hintCount {
  letter-spacing: 0;
  font-weight: 600;
  opacity: 0.7;
  margin-left: 8px;
}

/* Start screen list */
.startList {
  width: 100%;
  text-align: center;
}

.startList > * { margin-bottom: 24px; }

/* -------------------------------------------------
   Game controls (above board)
-------------------------------------------------- */

.gameControls {
  display: grid;
  grid-template-columns: 1fr 1fr 1fr;
  grid-template-areas: "how hint reset";
  gap: 10px;
  margin-bottom: 14px;
}

.gameControls .btn {
  width: 100%;
  max-width: none;
}

/* Force all controls into a single aligned row */
#howToBtn { grid-area: how; }
#hintBtn { grid-area: hint; }
#showWinBtn { grid-area: hint; }
#resetBtn { grid-area: reset; }

/* When you win, we hide the 3 control buttons */
.gameControls.is-won #howToBtn,
.gameControls.is-won #hintBtn,
.gameControls.is-won #resetBtn {
  display: none;
}

/* -------------------------------------------------
   Game screen
-------------------------------------------------- */

#gameScreen.screen--active {
  display: flex;
  flex-direction: column;
  align-items: center;
}

/* Make game screen a positioning context for dropped pieces */
#gameScreen {
  position: relative;
  overflow: hidden;
  touch-action: manipulation;
}

.gameStack {
  width: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
}

.boardArea {
  background: none;
  border: none;
  padding: 0;
  margin-top: 0;
  margin-bottom: 18px;
  width: 100%;
  display: flex;
  justify-content: center;
}

/* -------------------------------------------------
   Grid sizing: fit small phones
-------------------------------------------------- */

@media (max-width: 430px) { :root { --cell: 52px; } }
@media (max-width: 390px) { :root { --cell: 48px; } }
@media (max-width: 360px) { :root { --cell: 44px; } }

.gridWrap {
  position: relative; /* anchor modals */
  display: inline-block;
  max-width: 100%;
}

.grid {
  display: inline-grid;
  grid-template-columns: repeat(6, var(--cell));
  grid-template-rows: repeat(6, var(--cell));
  gap: var(--gap);

  padding: var(--grid-pad);
  border: var(--grid-border) solid var(--border);

  background: #faf8f3;
  border-radius: 18px;

  box-shadow: 0 2px 8px rgba(0,0,0,0.06);
  max-width: 100%;
}

/* Grid cells */
.cell {
  width: var(--cell);
  height: var(--cell);

  background: transparent;
  border: var(--tile-border) solid rgba(214, 207, 191, 0.55);
  border-radius: var(--tile-radius);

  position: relative;
}

.cell-mark {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  font-weight: 700;
  pointer-events: none;
  font-size: 24px;
  text-transform: uppercase;
  color: #d6cfbf;
  opacity: 1;
}

/* -------------------------------------------------
   Tray
-------------------------------------------------- */

.trayArea {
  margin-top: 18px;
  padding: 0;
  border: none;
  background: none;

  display: flex;
  flex-direction: column;

  /* IMPORTANT: stretch so the tray can use full width for edge alignment */
  align-items: stretch;

  width: 100%;
}

/* The tray now fills the same width as board/controls,
   and pushes first/last columns to the edges */
.tray {
  width: 100%;

  display: grid;
  grid-template-columns: repeat(3, max-content);
  gap: 12px;

  justify-content: space-between;

  min-height: calc((var(--cell) * 2) + 12px);
  align-content: start;
}

/* Each slot is exactly one omino footprint */
.tray-slot {
  width: calc(var(--cell) * 2 + var(--gap));
  min-height: var(--cell);
}

/* Progressive enhancement: expand slot when tile is vertical (Safari 16+ supports :has) */
@supports selector(:has(*)) {
  .tray-slot:has(.omino[data-orientation="v"]) {
    min-height: calc(var(--cell) * 2 + var(--gap));
  }
}

/* Fallback (no :has): allow taller content without clipping */
@supports not selector(:has(*)) {
  .tray-slot { height: auto; }
}

/* -------------------------------------------------
   Ominos
-------------------------------------------------- */

.omino {
  width: calc(var(--cell) * 2 + var(--gap));
  height: var(--cell);

  display: grid;
  grid-template-columns: 1fr 1fr;

  background: #ffffff;
  border: var(--tile-border) solid var(--border);
  border-radius: var(--tile-radius);

  box-shadow: 0 2px 6px rgba(0,0,0,0.08);
  position: relative;

  touch-action: none;
  cursor: grab;

  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
  -webkit-tap-highlight-color: transparent;
}

/* Divider line (horizontal orientation) */
.omino::after {
  content: "";
  position: absolute;
  top: 10%;
  bottom: 10%;
  left: calc(var(--cell) + (var(--gap) / 2));
  width: 1px;
  background: var(--border);
}

/* Vertical orientation */
.omino[data-orientation="v"] {
  width: var(--cell);
  height: calc(var(--cell) * 2 + var(--gap));

  grid-template-columns: 1fr;
  grid-template-rows: 1fr 1fr;
}

/* Divider line (vertical orientation) */
.omino[data-orientation="v"]::after {
  left: 10%;
  right: 10%;
  top: calc(var(--cell) + (var(--gap) / 2));
  height: 1px;
  width: auto;
}

.omino-letter {
  display: grid;
  place-items: center;
  font-size: 22px;
  font-weight: 700;
}

.omino.dragging {
  cursor: grabbing;
  z-index: 1000;
  transform: scale(1.02);
}

/* Locked hint tile: subtle shade + no grab cursor */
.omino.is-locked {
  background: #f6f3ea;
  cursor: default;
}

.omino.is-locked.dragging {
  transform: none;
}

/* Lock tiles after win */
#gameScreen.game-locked .omino { pointer-events: none; }

/* Reduce motion preference */
@media (prefers-reduced-motion: reduce) {
  .omino.dragging { transform: none; }
}

/* These three blocks should share the exact same outer width */
.gameControls,
.gridWrap,
.trayArea {
  width: var(--board-outer);
  max-width: 100%;
}

/* -------------------------------------------------
   Modal (How-to + Win)
-------------------------------------------------- */

.modal {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);

  background: #fff;
  padding: 18px;
  border-radius: 14px;
  width: min(340px, 92%);

  box-shadow: 0 12px 30px rgba(0,0,0,0.25);
  text-align: center;

  z-index: 50;
}

.modalBody {
  margin: 10px 0 14px;
}

.modalBody p {
  margin: 10px 0;
}

/* Preserve \n line breaks in win text (textContent uses newline chars) */
#winText {
  white-space: pre-line;
}

/* Backdrop that blocks interaction behind modals */
.modalBackDrop {
  position: absolute;
  inset: 0;
  z-index: 40;            /* below .modal (which is 50 in your CSS) */
}

/* If a modal is open, prevent interacting with the board/tray/controls behind it */
.gameStack.is-modal-open {
  pointer-events: none;
}

/* Re-enable pointer events for the modal + backdrop themselves */
.gameStack.is-modal-open .modal,
.gameStack.is-modal-open .modalBackDrop {
  pointer-events: auto;
}

/* -------------------------------------------------
   Logo tiles and date, puzzle number
-------------------------------------------------- */

.logoWrap { margin: 12px 0 24px; }

.logoOminos {
  display: flex;
  justify-content: center;
  gap: 4px;
}

.logoOmino {
  width: calc(var(--cell) * 2 + var(--gap));
  height: var(--cell);

  display: grid;
  grid-template-columns: 1fr 1fr;

  background: #fff;
  border: var(--tile-border) solid var(--border);
  border-radius: var(--tile-radius);

  box-shadow: 0 2px 6px rgba(0,0,0,0.08);
  position: relative;
}

.logoOmino::after {
  content: "";
  position: absolute;
  top: 12%;
  bottom: 12%;
  left: var(--cell);
  width: 1px;
  background: var(--border);
}

.logoLetter {
  display: grid;
  place-items: center;
  font-size: 20px;
  font-weight: 800;
}

.logoMeta {
  margin: 10px 0 0;
  text-align: center;
  font-size: 14px;
  opacity: 0.75; /* matches footer vibe */
}

.logoMetaSep {
  margin: 0 10px;
}

/* -------------------------------------------------
   Footer
-------------------------------------------------- */

.siteFooter {
  margin-top: auto;
  padding: 12px 0 18px;
  text-align: center;
  font-size: 14px;
  opacity: 0.75;
}

.siteFooter p { margin: 0; }

.siteFooter a {
  color: inherit;
  text-decoration: underline;
}

.siteFooter a:hover,
.siteFooter a:focus-visible {
  text-decoration: none;
}
