:root {
  --cream: #F4EEE0;
  --paper: #FDFBF5;
  --ink: #241F16;
  --muted: #8C8570;

  --forest: #1E3D34;
  --forest-dark: #17302A;
  --terracotta: #B5502E;
  --terracotta-dark: #963F22;
  --olive: #6B6A3F;
  --olive-dark: #565530;

  --teal: #3E7C74;
  --teal-circle: #DCE9E4;
  --terracotta-circle: #F5DCC9;
  --olive-circle: #E4E0C9;

  --gold: #9C7A3C;
  --line: rgba(36, 31, 22, 0.12);

  --tile-size: 46px;
  --layer-shift: 8px;

  --radius-lg: 20px;
  --radius-md: 14px;
  --radius-sm: 10px;
  --shadow-card: 0 1px 2px rgba(36, 31, 22, 0.04), 0 8px 24px rgba(36, 31, 22, 0.07);
  --shadow-card-hover: 0 2px 4px rgba(36, 31, 22, 0.06), 0 14px 32px rgba(36, 31, 22, 0.1);
}

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

html, body {
  height: 100%;
}

body {
  margin: 0;
  background: var(--cream);
  background-image:
    radial-gradient(ellipse 900px 500px at 8% 100%, rgba(30, 61, 52, 0.06), transparent 60%),
    radial-gradient(ellipse 700px 500px at 100% 0%, rgba(181, 80, 46, 0.05), transparent 60%);
  color: var(--ink);
  font-family: 'Space Mono', monospace;
  min-height: 100%;
  overscroll-behavior: none;
}

body.is-playing {
  height: 100dvh;
  overflow: hidden;
}

.page {
  width: 100%;
  max-width: 760px;
  margin: 0 auto;
  padding: 32px 20px 56px;
}

body.is-playing .page {
  max-width: none;
  height: 100dvh;
  padding: 10px;
  display: flex;
}

.panel {
  background: transparent;
  border: none;
  box-shadow: none;
  padding: 0;
}

.play-panel {
  flex: 1;
  min-height: 0;
  display: flex;
  flex-direction: column;
  padding: 10px 10px 14px;
  width: 100%;
  background: var(--paper);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-card);
}

.hidden {
  display: none !important;
}

.brand-row {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 16px;
  margin-bottom: 34px;
}

.brand-id {
  display: flex;
  align-items: center;
  gap: 16px;
}

.brand-mark {
  width: 46px;
  height: 46px;
  flex-shrink: 0;
  color: var(--forest);
}

.brand {
  font-family: 'Space Grotesk', sans-serif;
  font-weight: 700;
  font-size: 26px;
  line-height: 1.05;
  letter-spacing: -0.3px;
  color: var(--forest);
}

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

.settings-btn {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
  background: var(--paper);
  border: 1px solid var(--line);
  border-radius: var(--radius-md);
  box-shadow: var(--shadow-card);
  padding: 10px 16px;
  font-family: 'Space Grotesk', sans-serif;
  font-size: 11px;
  font-weight: 500;
  color: var(--ink);
  cursor: pointer;
  flex-shrink: 0;
}

.settings-btn svg {
  width: 20px;
  height: 20px;
  color: var(--muted);
}

.settings-btn:hover {
  box-shadow: var(--shadow-card-hover);
}

.difficulty-grid {
  display: grid;
  gap: 18px;
  grid-template-columns: repeat(3, 1fr);
}

.difficulty-card {
  text-align: center;
  background: var(--paper);
  border: 1px solid var(--line);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-card);
  padding: 28px 20px 22px;
  cursor: pointer;
  font-family: inherit;
  color: var(--ink);
  transition: transform 0.18s ease, box-shadow 0.18s ease;
  display: flex;
  flex-direction: column;
  align-items: center;
}

.difficulty-card:hover {
  transform: translateY(-3px);
  box-shadow: var(--shadow-card-hover);
}

.difficulty-icon {
  width: 76px;
  height: 76px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-family: 'Noto Sans Kawi', sans-serif;
  font-size: 34px;
  font-weight: 700;
  margin-bottom: 18px;
}

.difficulty-card[data-tone="teal"] .difficulty-icon { background: var(--teal-circle); color: var(--teal); }
.difficulty-card[data-tone="terracotta"] .difficulty-icon { background: var(--terracotta-circle); color: var(--terracotta); }
.difficulty-card[data-tone="olive"] .difficulty-icon { background: var(--olive-circle); color: var(--olive); }

.difficulty-title {
  font-family: 'Space Grotesk', sans-serif;
  font-weight: 700;
  font-size: 18px;
}

.difficulty-tagline {
  color: var(--muted);
  font-size: 12.5px;
  margin-top: 8px;
  line-height: 1.5;
  min-height: 38px;
}

.difficulty-badge {
  margin-top: 14px;
  font-size: 11.5px;
  font-weight: 700;
  padding: 6px 12px;
  border-radius: 999px;
  white-space: nowrap;
}

.difficulty-card[data-tone="teal"] .difficulty-badge { background: var(--teal-circle); color: var(--teal); }
.difficulty-card[data-tone="terracotta"] .difficulty-badge { background: var(--terracotta-circle); color: var(--terracotta); }
.difficulty-card[data-tone="olive"] .difficulty-badge { background: var(--olive-circle); color: var(--olive); }

.difficulty-start {
  margin-top: 20px;
  width: 100%;
  border: none;
  border-radius: var(--radius-md);
  padding: 13px 10px;
  font-family: 'Space Grotesk', sans-serif;
  font-weight: 700;
  font-size: 14px;
  color: var(--paper);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  transition: filter 0.15s ease, transform 0.15s ease;
}

.difficulty-start:hover {
  filter: brightness(1.08);
  transform: translateY(-1px);
}

.difficulty-card[data-tone="teal"] .difficulty-start { background: var(--forest); }
.difficulty-card[data-tone="terracotta"] .difficulty-start { background: var(--terracotta); }
.difficulty-card[data-tone="olive"] .difficulty-start { background: var(--olive); }

.difficulty-start::before {
  content: '';
  width: 0;
  height: 0;
  border-style: solid;
  border-width: 5px 0 5px 8px;
  border-color: transparent transparent transparent var(--paper);
}

.how-to {
  margin-top: 30px;
  font-size: 12.5px;
  color: var(--muted);
  line-height: 1.6;
  border-top: 1px solid var(--line);
  padding-top: 20px;
}

.quote-strip {
  margin-top: 24px;
  text-align: center;
  font-family: 'Space Grotesk', sans-serif;
  font-style: italic;
  color: var(--muted);
  font-size: 13px;
}

.play-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 10px;
  flex-wrap: wrap;
  flex-shrink: 0;
  padding: 4px 6px 0;
}

.level-id {
  display: flex;
  align-items: center;
  gap: 10px;
}

.icon-btn {
  width: 38px;
  height: 38px;
  flex-shrink: 0;
  border-radius: var(--radius-sm);
  border: 1px solid var(--line);
  background: var(--paper);
  box-shadow: var(--shadow-card);
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  color: var(--ink);
}

.icon-btn svg {
  width: 18px;
  height: 18px;
}

.icon-btn:hover {
  box-shadow: var(--shadow-card-hover);
}

.level-text {
  display: flex;
  flex-direction: column;
}

.level-name {
  font-family: 'Space Grotesk', sans-serif;
  font-weight: 700;
  font-size: 17px;
}

.level-sub {
  font-size: 11.5px;
  color: var(--muted);
}

.stat-row {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
}

.stat-box {
  border: 1px solid var(--line);
  background: var(--paper);
  box-shadow: var(--shadow-card);
  border-radius: var(--radius-sm);
  padding: 6px 12px;
  font-size: 12px;
  font-weight: 700;
  white-space: nowrap;
}

.stat-label {
  color: var(--muted);
  font-weight: 400;
  margin-right: 4px;
}

.board-wrapper {
  flex: 1;
  min-height: 0;
  margin: 10px 0;
  overflow: hidden;
  display: flex;
  align-items: center;
  justify-content: center;
}

.board-scaler {
  position: relative;
}

.board {
  position: relative;
}

.tile {
  position: absolute;
  width: var(--tile-size);
  height: calc(var(--tile-size) * 1.3);
  border: none;
  border-radius: 4px;
  display: block;
  font-family: inherit;
  cursor: default;
  padding: 0;
  overflow: visible;
  background: none;
  -webkit-appearance: none;
  appearance: none;
  touch-action: none;
  -webkit-user-select: none;
  user-select: none;
}

.tile-face {
  position: absolute;
  inset: 0;
  border-radius: 4px;
  border: none;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  overflow: hidden;
  background-color: #FBF7EF;
  padding: 5% 8%;
  box-shadow:
    inset -2px -2px 5px 1px #cabf9fbb,
    inset 2px 2px 3px 1px #fffdf7,
    1px 1px 0 1px #d9cda9,
    2px 2px 0 1px #d9cda9,
    3px 3px 0 1px var(--tile-edge, #cabf9f),
    var(--layer-shadow, 3px 4px 6px 1px rgba(36, 31, 22, 0.25));
  transition: transform 0.15s ease, box-shadow 0.15s ease;
}

.tile-aksara .tile-face { --tile-edge: #7fada3; }
.tile-latin .tile-face { --tile-edge: #c98d63; }

.tile-face::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  height: 3px;
  background: linear-gradient(90deg, #E3D3A6 0%, #D3BE8B 50%, #E3D3A6 100%);
  border-radius: 4px 4px 0 0;
}

.tile.is-peeking .tile-face {
  transform: scale(1.35) translateY(-4px);
  box-shadow:
    inset -2px -2px 5px 1px #cabf9fbb,
    inset 2px 2px 3px 1px #fffdf7,
    1px 1px 0 1px #d9cda9,
    2px 2px 0 1px #d9cda9,
    3px 3px 0 1px var(--tile-edge, #cabf9f),
    0 12px 24px rgba(23, 19, 12, 0.5);
  outline: 2px solid var(--gold);
  outline-offset: -2px;
}

.tile.layer-0 { --layer-shadow: 3px 4px 6px 1px rgba(36, 31, 22, 0.22); }
.tile.layer-1 { --layer-shadow: 4px 5px 7px 1px rgba(36, 31, 22, 0.26); }
.tile.layer-2 { --layer-shadow: 4px 6px 8px 2px rgba(36, 31, 22, 0.3); }
.tile.layer-3 { --layer-shadow: 5px 7px 9px 2px rgba(36, 31, 22, 0.34); }
.tile.layer-4 { --layer-shadow: 5px 8px 10px 2px rgba(36, 31, 22, 0.38); }

.tile-glyph {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 100%;
  flex: 1;
  min-height: 0;
  line-height: 1;
  font-weight: 700;
}

.tile-aksara .tile-glyph {
  font-family: 'Noto Sans Kawi', sans-serif;
  color: var(--teal);
}

.tile-aksara .tile-glyph span {
  display: inline-block;
  line-height: 1;
  transform: translateY(12%);
  white-space: nowrap;
}

.tile-latin .tile-glyph {
  font-family: 'Space Grotesk', sans-serif;
  text-transform: lowercase;
  color: var(--terracotta);
}

.tile-latin .tile-glyph span {
  display: inline-block;
  line-height: 1;
  white-space: nowrap;
}

.tile-gloss {
  font-size: 6.5px;
  color: var(--ink);
  opacity: 0.5;
  margin-top: 2%;
  text-transform: uppercase;
  letter-spacing: 0.02em;
  flex-shrink: 0;
  white-space: nowrap;
  max-width: 100%;
  line-height: 1.15;
  text-align: center;
}

.tile.is-locked .tile-face {
  filter: grayscale(0.6) brightness(0.9);
}

.tile.is-locked {
  cursor: not-allowed;
}

.tile.is-open {
  cursor: pointer;
}

.tile.is-open:hover .tile-face {
  transform: translate(-1px, -2px);
}

.tile.is-selected .tile-face {
  outline: 3px solid var(--terracotta);
  outline-offset: -2px;
}

.tile.is-mismatch .tile-face {
  animation: shake 0.4s ease;
  background: var(--terracotta-circle) !important;
}

.tile.is-hint .tile-face {
  animation: pulseGlow 0.7s ease infinite;
}

@keyframes shake {
  0%, 100% { transform: translateX(0); }
  25% { transform: translateX(-3px); }
  75% { transform: translateX(3px); }
}

@keyframes pulseGlow {
  0%, 100% { outline: 3px solid var(--gold); outline-offset: -2px; }
  50% { outline: 3px solid transparent; outline-offset: -2px; }
}

.action-row {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
  flex-shrink: 0;
}

.action-btn {
  flex: 1;
  min-width: 90px;
  border: none;
  border-radius: var(--radius-md);
  background: var(--forest);
  box-shadow: var(--shadow-card);
  padding: 12px 8px;
  font-family: 'Space Grotesk', sans-serif;
  font-weight: 700;
  font-size: 12.5px;
  cursor: pointer;
  color: var(--paper);
  transition: filter 0.15s ease, transform 0.15s ease;
}

.action-btn:hover {
  filter: brightness(1.1);
  transform: translateY(-1px);
}

.action-btn.ghost {
  background: transparent;
  border: 1px solid var(--line);
  color: var(--ink);
  box-shadow: none;
}

.toast {
  position: fixed;
  bottom: 18px;
  left: 50%;
  transform: translate(-50%, 20px);
  background: var(--forest);
  color: var(--paper);
  padding: 11px 18px;
  border-radius: var(--radius-md);
  font-size: 12.5px;
  opacity: 0;
  transition: all 0.2s ease;
  pointer-events: none;
  max-width: 90%;
  text-align: center;
  z-index: 1200;
  box-shadow: var(--shadow-card-hover);
}

.toast.is-visible {
  opacity: 1;
  transform: translate(-50%, 0);
}

.overlay {
  position: fixed;
  inset: 0;
  background: rgba(36, 31, 22, 0.5);
  backdrop-filter: blur(2px);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
  z-index: 999;
}

.modal {
  background: var(--paper);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-card-hover);
  padding: 28px;
  width: 100%;
  max-width: 360px;
}

.modal h2 {
  font-family: 'Space Grotesk', sans-serif;
  color: var(--forest);
  margin: 0 0 4px;
}

.modal-sub {
  color: var(--muted);
  font-size: 12.5px;
  margin-bottom: 18px;
}

.result-rows {
  display: grid;
  gap: 10px;
  margin-bottom: 20px;
}

.result-row {
  display: flex;
  justify-content: space-between;
  border-bottom: 1px solid var(--line);
  padding-bottom: 8px;
  font-size: 13px;
}

.result-row b {
  font-family: 'Space Grotesk', sans-serif;
}

.modal-actions {
  display: flex;
  gap: 10px;
}

@media (max-width: 720px) {
  .difficulty-grid {
    grid-template-columns: 1fr;
  }
}

@media (max-width: 480px) {
  .page {
    padding: 22px 14px 40px;
  }
  .brand-row {
    margin-bottom: 22px;
  }
  .brand {
    font-size: 21px;
  }
  body.is-playing .page {
    padding: 6px;
  }
  .play-panel {
    padding: 8px 8px 10px;
  }
  .level-name {
    font-size: 15px;
  }
  .stat-box {
    font-size: 10.5px;
    padding: 5px 9px;
  }
  .action-btn {
    font-size: 11px;
    padding: 10px 6px;
    min-width: 70px;
  }
}
