:root {
  --bg: #f6f4ef;
  --panel: #ffffff;
  --line: #dfd8cb;
  --ink: #1f1c16;
  --muted: #696153;
  --accent: #1f8f4f;
  --accent-press: #176b3b;
  --lock: #a09a8f;
  --cell: #faf4e7;
  --cell-active: #f6c453;
}

* {
  box-sizing: border-box;
  -webkit-tap-highlight-color: transparent;
  user-select: none;
}

html,
body {
  margin: 0;
  padding: 0;
  width: 100%;
  height: 100%;
  overflow: hidden;
  font-family: "Trebuchet MS", "Gill Sans", sans-serif;
  background: var(--bg);
  color: var(--ink);
}

.app {
  height: 100%;
  padding: calc(14px + env(safe-area-inset-top, 0px)) 14px calc(14px + env(safe-area-inset-bottom, 0px));
}

.screen {
  height: 100%;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

/* Scrolly GameModal back control sits top-right over the WebView — clear that band */
#hub-screen {
  padding-top: clamp(40px, 11vmin, 56px);
}

.hidden {
  display: none !important;
}

.muted {
  margin: 0;
  color: var(--muted);
  font-size: 13px;
}

.hub-status {
  margin: 0;
  min-height: 0;
  font-size: 13px;
  text-align: center;
}

.hub-status:empty {
  display: none;
}

.levels-list {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 10px;
  align-content: start;
  align-items: start;
  width: 100%;
  max-width: min(100%, 400px);
  margin: 0 auto;
  padding: 0 2px;
}

.level-card {
  aspect-ratio: 1;
  width: 100%;
  margin: 0;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: var(--panel);
  color: var(--ink);
  padding: 8px 6px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 4px;
  text-align: center;
}

.level-card.unlocked {
  cursor: pointer;
}

.level-card.unlocked:active {
  transform: scale(0.98);
}

.level-card.locked {
  color: var(--lock);
  opacity: 0.78;
}

.level-card__n {
  font-size: clamp(24px, 9vmin, 36px);
  font-weight: 800;
  line-height: 1;
  color: var(--ink);
}

.level-card.locked .level-card__n {
  color: var(--lock);
}

.level-card__hint {
  font-size: 10px;
  font-weight: 700;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: var(--muted);
}

.level-card__st {
  font-size: 11px;
  font-weight: 700;
  color: var(--muted);
  line-height: 1.2;
}

.level-card.unlocked .level-card__st {
  color: var(--accent);
}

.level-card.locked .level-card__st {
  color: var(--lock);
}

.level-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  flex-shrink: 0;
}

.level-header--levels-nav {
  justify-content: flex-start;
  width: 100%;
  padding-top: clamp(36px, 10vmin, 52px);
  padding-bottom: 2px;
}

/* Text control: return to level hub (distinct from native modal “close game” chevron). */
.levels-nav-btn {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  padding: 8px 14px 8px 10px;
  margin: 0;
  border-radius: 12px;
  border: 1px solid var(--line);
  background: var(--panel);
  color: var(--ink);
  font-family: inherit;
  font-size: 15px;
  font-weight: 700;
  letter-spacing: 0.01em;
  cursor: pointer;
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
  box-shadow: 0 1px 2px rgba(31, 28, 22, 0.05);
}

.levels-nav-btn:active {
  transform: scale(0.98);
  background: #f3efe6;
}

.levels-nav-btn__icon {
  flex-shrink: 0;
  color: var(--accent);
  opacity: 0.95;
}

.levels-nav-btn__label {
  line-height: 1.2;
}

.eye-btn {
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  width: 44px;
  height: 44px;
  padding: 0;
  border-radius: 10px;
  border: 1px solid var(--line);
  background: var(--panel);
  color: var(--ink);
  cursor: pointer;
  touch-action: manipulation;
}

.eye-btn:active {
  transform: scale(0.96);
}

.eye-btn .eye-icon {
  display: block;
}

.eye-btn .eye-icon.hidden {
  display: none;
}

.ghost-btn,
.primary-btn {
  border-radius: 10px;
  font-size: 14px;
  padding: 10px 12px;
  border: 1px solid var(--line);
}

.ghost-btn {
  background: var(--panel);
  color: var(--ink);
}

.primary-btn {
  background: var(--accent);
  color: #fff;
  border-color: transparent;
}

.primary-btn:active {
  background: var(--accent-press);
}

/*
 * Square grid: side = min(max CSS width, viewport). Do not cap max-height below
 * that side or rows get clipped (8×8 must stay as tall as wide).
 */
.board {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 0;
  box-sizing: border-box;
  width: min(92vw, 384px, 88vmin);
  max-width: 100%;
  aspect-ratio: 1 / 1;
  height: auto;
  border: 1px solid #d5cab2;
  touch-action: none;
  position: relative;
  overflow: hidden;
}

.board.board--compact .cell {
  font-size: 32px;
  font-weight: 500;
}

.cell {
  border: 1px solid #d5cab2;
  background: var(--cell);
  border-radius: 0;
  aspect-ratio: 1 / 1;
  min-height: 0;
  font-size: 48px;
  font-weight: 600;
  color: #3a2f1d;
  margin: 0;
  touch-action: none;
  position: relative;
  z-index: 1;
}

.cell.active {
  background: #f2ead8;
  border-color: #d6c59f;
  color: #887651;
}

.cell.found {
  color: #7d6b43;
  /* No line-through on single-letter buttons: WebKit draws a tiny strike per glyph on all cells. */
  background: #efe8da;
}

.trail {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
  z-index: 2;
}

.trail path {
  fill: none;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.trail path.found {
  stroke: #7d6b43;
  stroke-width: 4;
  opacity: 0.75;
}

.trail path.current {
  stroke: #6a5732;
  stroke-width: 5;
  opacity: 0.95;
}

.level-progress {
  margin: 0;
  font-size: 13px;
  text-align: center;
  width: 100%;
}

.word-bank {
  display: flex;
  flex-wrap: wrap;
  gap: 6px 8px;
  justify-content: center;
  flex-shrink: 0;
  width: 100%;
  max-width: min(96vw, 420px);
  max-height: min(120px, 28vh);
  overflow-y: auto;
  padding: 2px;
  -webkit-overflow-scrolling: touch;
}

/* [hidden] loses to display:flex in some WebViews; !important matches RN WebView */
.word-bank.word-bank--hidden {
  display: none !important;
  max-height: 0 !important;
  padding: 0 !important;
  margin: 0 !important;
  overflow: hidden !important;
}

.word-chip {
  font-size: 12px;
  font-weight: 700;
  letter-spacing: 0.02em;
  padding: 4px 8px;
  border-radius: 8px;
  border: 1px solid var(--line);
  background: var(--panel);
  color: var(--ink);
}

.word-chip--found {
  color: var(--muted);
  border-color: #cfc7b8;
}

.word-chip--found s {
  text-decoration: line-through;
  text-decoration-thickness: 2px;
}

/* Back stays top-left; rest is centered as one column. */
#level-screen {
  gap: 0;
}

.level-main {
  flex: 1;
  min-height: 0;
  width: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 4px 0 calc(10px + env(safe-area-inset-bottom, 0px));
  overflow-y: auto;
  overflow-x: hidden;
  -webkit-overflow-scrolling: touch;
}

.level-main-inner {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 10px;
  width: 100%;
  max-width: min(96vw, 420px);
}

.playfield {
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  justify-content: center;
  width: 100%;
}

@media (max-width: 420px) {
  .cell {
    font-size: 44px;
  }
  .board.board--compact .cell {
    font-size: 28px;
  }
}

@media (min-width: 700px) {
  .app {
    padding: calc(18px + env(safe-area-inset-top, 0px)) 22px calc(18px + env(safe-area-inset-bottom, 0px));
  }

  .levels-list {
    max-width: min(100%, 560px);
    gap: 14px;
  }

  .level-card {
    border-radius: 14px;
    padding: 12px 8px;
    gap: 6px;
  }

  .level-card__n {
    font-size: clamp(28px, 6vmin, 42px);
  }

  .level-card__hint,
  .level-card__st {
    font-size: 12px;
  }

  .levels-nav-btn {
    padding: 10px 18px 10px 12px;
    font-size: 17px;
    border-radius: 14px;
  }

  .eye-btn {
    width: 52px;
    height: 52px;
    border-radius: 12px;
  }

  .board {
    width: min(78vw, 560px, 72vmin);
  }

  .cell {
    font-size: 56px;
  }

  .board.board--compact .cell {
    font-size: 38px;
  }

  .trail path.found {
    stroke-width: 5;
  }

  .trail path.current {
    stroke-width: 6;
  }

  .word-bank {
    max-width: min(90vw, 560px);
    max-height: min(140px, 24vh);
    gap: 8px 10px;
  }

  .word-chip {
    font-size: 14px;
    padding: 6px 10px;
    border-radius: 10px;
  }

  .level-main-inner {
    max-width: min(90vw, 580px);
    gap: 14px;
  }

  .level-progress {
    font-size: 15px;
  }
}

@media (min-width: 900px) {
  .levels-list {
    max-width: min(100%, 680px);
    gap: 16px;
  }

  .board {
    width: min(68vw, 680px, 68vmin);
  }

  .cell {
    font-size: 64px;
  }

  .board.board--compact .cell {
    font-size: 44px;
  }

  .word-bank,
  .level-main-inner {
    max-width: min(84vw, 700px);
  }
}
