:root {
  --bg1: #eef3fb;
  --bg2: #f7eefb;
  --ink: #1e2235;
  --muted: #6b7280;
  --accent: #6d5efc;
  --accent2: #22b8a8;
  --tile: #fdfbf4;
  --tile-edge: #e2dcc9;
  --tw: #f6c453;
  --dw: #f3d07a;
  --tl: #7fcfc0;
  --dl: #bcd9f4;
  --star: #ffd7e0;
  --radius: 18px;
  --shadow: 0 10px 30px rgba(31, 41, 90, 0.10);
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  min-height: 100%;
  color: var(--ink);
  font-family: "IBM Plex Sans Thai", "Noto Sans Thai", "Sarabun", system-ui, sans-serif;
  background:
    radial-gradient(1100px 600px at 12% -10%, #dbe7ff 0%, transparent 60%),
    radial-gradient(900px 500px at 95% 0%, #ffe3f3 0%, transparent 55%),
    linear-gradient(160deg, var(--bg1), var(--bg2));
  -webkit-text-size-adjust: 100%;
}

.app {
  max-width: 620px;
  margin: 0 auto;
  padding: 14px 12px calc(18px + env(safe-area-inset-bottom));
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.glass {
  background: rgba(255, 255, 255, 0.72);
  border: 1px solid rgba(255, 255, 255, 0.9);
  backdrop-filter: blur(14px);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 14px;
}

.brand { display: flex; align-items: center; gap: 10px; }
.logo {
  width: 40px; height: 40px;
  display: grid; place-items: center;
  border-radius: 12px;
  font-weight: 700; font-size: 20px;
  color: #fff;
  background: linear-gradient(140deg, var(--accent), var(--accent2));
  box-shadow: 0 6px 16px rgba(109, 94, 252, 0.35);
}
h1 { font-size: 20px; margin: 0; letter-spacing: 0.2px; }
.sub { margin: 0; font-size: 11.5px; color: var(--muted); }

.scores {
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  gap: 8px;
  align-items: center;
}
.score {
  padding: 8px 12px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  transition: transform 0.25s ease, box-shadow 0.25s ease;
}
.score.active {
  transform: translateY(-2px);
  box-shadow: 0 12px 26px rgba(109, 94, 252, 0.25);
  outline: 2px solid rgba(109, 94, 252, 0.35);
}
.score .who { font-size: 11px; color: var(--muted); }
.score strong { font-size: 22px; line-height: 1; }
.turn { font-size: 11.5px; color: var(--muted); text-align: center; }

.board-wrap { padding: 8px; }
.board {
  display: grid;
  grid-template-columns: repeat(var(--cols, 15), 1fr);
  gap: 2px;
  width: 100%;
}

.cell {
  position: relative;
  border-radius: 6px;
  background: #f4f6fb;
  border: 1px solid #e7ebf3;
  display: grid;
  place-items: center;
  font-size: 8px;
  color: #9aa3b2;
  cursor: pointer;
  transition: transform 0.12s ease, box-shadow 0.12s ease;
  user-select: none;
  overflow: hidden;
}
.cell.m-TW { background: var(--tw); color: #7a5b00; }
.cell.m-DW { background: var(--dw); color: #7a5b00; }
.cell.m-TL { background: var(--tl); color: #10665b; }
.cell.m-DL { background: var(--dl); color: #245a86; }
.cell.m-star { background: var(--star); color: #a33350; font-size: 13px; }
.cell.m-W2 { background: #ffd9e4; }
.cell.m-W3 { background: #f6a8a8; }
.cell.m-L2 { background: #cfe4f7; }
.cell.m-L3 { background: #a5e3cf; }
.cell.m-L4 { background: #f9c98f; }
.cell.m-L5 { background: #d3bcf7; }
.cell.m-W2 { color: #7a1f3d; font-weight: 700; }
.cell.m-W3 { color: #7d0f0f; font-weight: 700; }
.cell.m-L2 { color: #10395f; font-weight: 700; }
.cell.m-L3 { color: #0a4331; font-weight: 700; }
.cell.m-L4 { color: #653200; font-weight: 700; }
.cell.m-L5 { color: #341a63; font-weight: 700; }
.cell.center { box-shadow: inset 0 0 0 2px #ff8aa8; }
.cell { aspect-ratio: 1; font-size: 7px; }
.tile .pair { position: absolute; left: 2px; top: 1px; font-size: 6px; color: #8b8570; }
.cell:hover { transform: translateY(-1px); }
.cell.target { box-shadow: inset 0 0 0 2px var(--accent); }

.tile {
  width: 100%; height: 100%;
  border-radius: 6px;
  background: linear-gradient(180deg, #ffffff, var(--tile));
  border: 1px solid var(--tile-edge);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  color: #2a2a1f;
  font-weight: 600;
  line-height: 1;
  position: relative;
}
.tile .top { font-size: 8px; height: 9px; }
.tile .mid { font-size: 15px; display: flex; align-items: baseline; }
.tile .bot { font-size: 8px; height: 9px; }
.tile .val {
  position: absolute; right: 2px; bottom: 1px;
  font-size: 7px; color: #8b8570; font-weight: 500;
}
.tile.pending { background: linear-gradient(180deg, #fff, #eef0ff); border-color: var(--accent); }

.rack {
  display: flex;
  gap: 6px;
  padding: 8px;
  overflow-x: auto;
  scrollbar-width: none;
}
.rack::-webkit-scrollbar { display: none; }

.rack .tile {
  width: 40px; height: 44px;
  flex: 0 0 auto;
  border-radius: 9px;
  box-shadow: 0 4px 10px rgba(31, 41, 90, 0.12);
  cursor: pointer;
  transition: transform 0.15s ease;
}
.rack .tile .mid { font-size: 20px; }
.rack .tile.selected {
  transform: translateY(-6px);
  box-shadow: 0 10px 18px rgba(109, 94, 252, 0.35);
  border-color: var(--accent);
}

.palette {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  padding: 0 8px 8px;
}
.palette button {
  min-width: 38px; height: 38px;
  border-radius: 9px;
  border: 1px solid #e2e6f0;
  background: #fff;
  font-size: 15px;
  color: var(--ink);
  cursor: pointer;
}
.palette button.on { background: var(--accent); color: #fff; border-color: var(--accent); }

.actions { display: grid; grid-template-columns: 1.3fr 1fr 1fr 1fr; gap: 6px; padding: 0 8px 10px; }

.btn {
  border: 0;
  border-radius: 12px;
  padding: 11px 12px;
  font-size: 14px;
  font-weight: 600;
  font-family: inherit;
  cursor: pointer;
  transition: transform 0.12s ease, filter 0.12s ease;
}
.btn:active { transform: scale(0.98); }
.btn-primary { color: #fff; background: linear-gradient(140deg, var(--accent), #8b7dff); box-shadow: 0 8px 18px rgba(109, 94, 252, 0.3); }
.btn-soft { background: #eef0f8; color: #3c4257; }
.btn-ghost { background: rgba(255, 255, 255, 0.6); color: var(--accent); border: 1px solid #e0e3f0; font-size: 12.5px; padding: 8px 12px; }
.btn:disabled { opacity: 0.45; cursor: not-allowed; }

.preview {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 12px;
  font-size: 12px;
  min-height: 40px;
}
.preview-label { color: var(--muted); }
.preview-words { font-weight: 600; flex: 1; }
.preview-score { color: var(--accent2); font-weight: 700; }

.toast {
  position: fixed;
  left: 50%;
  bottom: calc(16px + env(safe-area-inset-bottom));
  transform: translate(-50%, 20px);
  background: #1e2235;
  color: #fff;
  padding: 10px 16px;
  border-radius: 999px;
  font-size: 13px;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s ease, transform 0.2s ease;
  max-width: 90vw;
  text-align: center;
  z-index: 30;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }

@media (max-width: 380px) {
  .tile .mid { font-size: 13px; }
  .rack .tile { width: 36px; height: 40px; }
}
