:root {
  --ink: #182226;
  --muted: #647077;
  --paper: #f4f0e8;
  --panel: #fffaf0;
  --line: #c9c0ae;
  --tile: #efd38f;
  --tile-edge: #b3893b;
  --accent: #1f7a6d;
  --accent-strong: #14574d;
  --danger: #b53636;
  --letter-2: #5aa0c8;
  --letter-3: #2a6f9e;
  --word-2: #d46a7e;
  --word-3: #b53636;
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

* {
  box-sizing: border-box;
}

body {
  min-height: 100vh;
  margin: 0;
  background: var(--paper);
  color: var(--ink);
}

button,
input {
  font: inherit;
}

button {
  border: 1px solid var(--line);
  background: #fff;
  color: var(--ink);
  min-height: 42px;
  border-radius: 8px;
  padding: 0 14px;
  cursor: pointer;
}

button:disabled {
  cursor: wait;
  opacity: 0.65;
}

.topbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 16px;
  padding: max(16px, env(safe-area-inset-top)) 18px 12px;
  background: var(--ink);
  color: #fff;
}

.topbar h1 {
  margin: 0;
  font-size: clamp(20px, 4vw, 30px);
  letter-spacing: 0;
}

.topbar p {
  margin: 4px 0 0;
  color: #dce2e2;
  font-size: 14px;
}

.icon-button {
  width: 44px;
  height: 44px;
  padding: 0;
  border-color: rgba(255, 255, 255, 0.35);
  background: rgba(255, 255, 255, 0.1);
  color: #fff;
  font-size: 26px;
  line-height: 1;
}

.app-shell {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 370px;
  gap: 18px;
  padding: 18px;
  max-width: 1280px;
  margin: 0 auto;
}

.workspace,
.side-panel {
  min-width: 0;
}

.toolbar,
.panel-actions,
.crop-controls,
.legend {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
}

.toolbar {
  margin-bottom: 12px;
}

.file-button {
  position: relative;
  display: inline-flex;
  align-items: center;
  min-height: 42px;
  padding: 0 14px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fff;
  cursor: pointer;
}

.file-button input {
  position: absolute;
  inset: 0;
  opacity: 0;
  cursor: pointer;
}

.image-tools {
  margin-bottom: 14px;
  padding: 12px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: rgba(255, 250, 240, 0.8);
}

#photoCanvas {
  display: block;
  width: 100%;
  max-height: 360px;
  border-radius: 6px;
  background: #101719;
}

.crop-controls {
  margin-top: 10px;
}

.crop-controls label {
  display: grid;
  grid-template-columns: auto minmax(90px, 1fr);
  align-items: center;
  gap: 7px;
  color: var(--muted);
  font-size: 13px;
}

.crop-controls select {
  min-height: 34px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fff;
  color: var(--ink);
  padding: 0 8px;
}

.status-line,
.muted {
  margin: 8px 0 0;
  color: var(--muted);
  font-size: 13px;
  line-height: 1.4;
}

.board-wrap {
  overflow-x: auto;
  padding-bottom: 4px;
}

.board {
  display: grid;
  grid-template-columns: repeat(15, minmax(24px, 1fr));
  width: min(100%, 720px);
  aspect-ratio: 1 / 1;
  border: 2px solid #736958;
  background: #736958;
  gap: 1px;
  touch-action: manipulation;
}

.cell {
  position: relative;
  display: grid;
  place-items: center;
  min-width: 0;
  min-height: 0;
  border: 0;
  border-radius: 0;
  padding: 0;
  background: #dfd0b5;
  color: var(--ink);
  font-weight: 800;
  font-size: clamp(13px, 2.7vw, 24px);
  line-height: 1;
}

.cell[data-bonus="2l"] {
  background: #79b4d1;
}

.cell[data-bonus="3l"] {
  background: #2a6f9e;
  color: #fff;
}

.cell[data-bonus="2w"] {
  background: #e08b9b;
}

.cell[data-bonus="3w"] {
  background: #b53636;
  color: #fff;
}

.cell.start::after {
  content: "★";
  position: absolute;
  font-size: 16px;
  color: #7a5c00;
}

.cell.has-letter {
  background: linear-gradient(#f8dda0, var(--tile));
  color: #342615;
  box-shadow: inset 0 -2px 0 var(--tile-edge);
}

.cell.has-letter::after {
  content: attr(data-score);
  position: absolute;
  right: 3px;
  bottom: 2px;
  font-size: 9px;
  font-weight: 700;
  color: #6c501d;
}

.cell.selected {
  outline: 3px solid var(--accent);
  outline-offset: -3px;
  z-index: 2;
}

.cell.ocr-low {
  outline: 3px dashed #f2c94c;
  outline-offset: -4px;
}

.cell.preview {
  outline: 3px solid #111;
  outline-offset: -3px;
}

.cell.new-tile {
  background: linear-gradient(#d9f0e7, #a9dbc9);
}

.bonus-label {
  font-size: clamp(8px, 1.8vw, 11px);
  font-weight: 800;
  color: rgba(24, 34, 38, 0.72);
}

.cell[data-bonus="3l"] .bonus-label,
.cell[data-bonus="3w"] .bonus-label {
  color: rgba(255, 255, 255, 0.9);
}

.keyboard {
  display: grid;
  grid-template-columns: repeat(8, minmax(34px, 1fr));
  gap: 6px;
  max-width: 720px;
  margin-top: 12px;
}

.keyboard button {
  min-height: 38px;
  padding: 0;
  font-weight: 800;
}

.side-panel {
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.panel-block,
.results-head {
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--panel);
  padding: 14px;
}

.panel-block.compact {
  padding: 12px;
}

.field-label {
  display: block;
  margin-bottom: 7px;
  font-size: 13px;
  font-weight: 700;
  color: var(--muted);
}

.rack-input {
  width: 100%;
  min-height: 48px;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 0 12px;
  background: #fff;
  font-size: 20px;
  font-weight: 800;
  letter-spacing: 0;
  text-transform: uppercase;
}

.rack-preview {
  display: flex;
  gap: 6px;
  min-height: 40px;
  margin-top: 10px;
}

.rack-tile {
  display: grid;
  place-items: center;
  width: 36px;
  height: 36px;
  border-radius: 6px;
  background: linear-gradient(#f8dda0, var(--tile));
  box-shadow: inset 0 -2px 0 var(--tile-edge);
  font-weight: 900;
}

.primary-button {
  flex: 1;
  border-color: var(--accent-strong);
  background: var(--accent);
  color: #fff;
  font-weight: 800;
}

.legend span {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  font-size: 12px;
  font-weight: 800;
}

.legend i {
  display: inline-block;
  width: 14px;
  height: 14px;
  border-radius: 3px;
}

.bonus-2l {
  background: var(--letter-2);
}

.bonus-3l {
  background: var(--letter-3);
}

.bonus-2w {
  background: var(--word-2);
}

.bonus-3w {
  background: var(--word-3);
}

.results-head {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 10px;
}

.results-head h2 {
  margin: 0;
  font-size: 18px;
}

#metaText {
  color: var(--muted);
  font-size: 12px;
  text-align: right;
}

.results {
  display: flex;
  flex-direction: column;
  gap: 8px;
  list-style: none;
  margin: 0;
  padding: 0;
}

.move button {
  display: grid;
  grid-template-columns: 54px 1fr;
  grid-template-areas:
    "score word"
    "score place";
  width: 100%;
  min-height: 66px;
  text-align: left;
  gap: 2px 10px;
  background: #fff;
}

.score {
  grid-area: score;
  display: grid;
  place-items: center;
  align-self: center;
  width: 46px;
  height: 46px;
  border-radius: 8px;
  background: var(--ink);
  color: #fff;
  font-size: 18px;
  font-weight: 900;
}

.word {
  grid-area: word;
  min-width: 0;
  overflow-wrap: anywhere;
  font-size: 18px;
  font-weight: 900;
}

.place {
  grid-area: place;
  color: var(--muted);
  font-size: 13px;
}

@media (max-width: 920px) {
  .app-shell {
    grid-template-columns: 1fr;
    padding: 12px;
  }

  .side-panel {
    order: 2;
  }

  .board {
    width: min(100%, calc(100vw - 24px));
  }
}

@media (max-width: 520px) {
  .topbar {
    padding-inline: 12px;
  }

  .topbar p {
    display: none;
  }

  .toolbar button,
  .file-button {
    flex: 1;
    justify-content: center;
    padding-inline: 8px;
  }

  .keyboard {
    grid-template-columns: repeat(7, 1fr);
  }

  .cell.has-letter::after {
    font-size: 7px;
  }
}
