:root {
  --bg: #EEF1F6;
  --cell: #FFFFFF;
  --cell-selected: #8FB4FF;
  --cell-same: #A6C8FF;
  --cell-peer: #EBF1FB;
  --note-hl: #BBD3FF;
  --given: #1F2937;
  --user: #2F6BFF;
  --error: #EF4444;
  --note: #6B7280;
  --accent: #2F6BFF;
  --muted: #9AA3B2;
  --text: #2B3445;
  --text2: #5B6472;
  --icon: #3A4252;
  --frame: #344861;
  --card: #FFFFFF;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; height: 100%; background: var(--bg); color: var(--text);
  font-family: "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif; overflow: hidden; touch-action: manipulation; }
button { font: inherit; }

/* ---- Bố cục ----
   Dọc : 1 cột, tối đa 540px, bảng ở giữa.
   Ngang: 2 cột lấp đầy màn hình (vd. 3120x1440) — bảng trọn cột trái, điều khiển cột phải. */
#root {
  height: 100%; height: 100dvh;
  margin: 0 auto; max-width: 540px;
  padding: 10px 14px;
  display: grid;
  grid-template-rows: auto auto auto auto 1fr auto auto auto;
  grid-template-columns: 1fr;
  row-gap: 12px; column-gap: 16px;
}
#topbar   { grid-row: 1; }
#score    { grid-row: 2; }
#status   { grid-row: 3; }
#zoombar  { grid-row: 4; }
#boardhost{ grid-row: 5; }
#actions  { grid-row: 6; }
#pad      { grid-row: 7; }
#apk      { grid-row: 8; text-align: center; font-size: 13px; color: var(--text2); text-decoration: none; }
#apk:hover { color: var(--accent); text-decoration: underline; }

@media (orientation: landscape) and (min-width: 600px) {
  #root { max-width: none; grid-template-columns: 1fr 1fr; }
  #boardhost { grid-column: 1; grid-row: 1 / span 8; }
  #topbar, #score, #status, #zoombar, #actions, #pad, #apk { grid-column: 2; }
}

/* ---- Thanh trên ---- */
#topbar { display: grid; grid-template-columns: auto 1fr auto auto auto; column-gap: 6px; align-items: center; }
#topbar .back { font-size: 30px; color: var(--text2); padding: 0 4px; line-height: 1; }
#topbar .title { font-size: 20px; font-weight: 700; color: var(--text); margin-left: 2px; }
.iconbtn { background: transparent; border: 0; color: var(--text2); font-size: 20px; width: 40px; height: 36px; cursor: pointer; }

#score { text-align: center; font-size: 26px; font-weight: 700; color: var(--accent); }

#status { display: grid; grid-template-columns: 1fr auto 1fr; align-items: center; font-size: 15px; color: var(--text2); }
#status .right { display: flex; justify-content: flex-end; align-items: center; gap: 6px; }
#difficulty { background: transparent; border: 0; color: var(--text); font-weight: 700; font-size: 15px; padding: 0 8px; cursor: pointer; }
#pause { background: transparent; border: 0; color: var(--text2); font-size: 16px; width: 34px; cursor: pointer; }

#zoombar { display: flex; justify-content: flex-end; gap: 10px; }
.zoombtn { width: 38px; height: 38px; border-radius: 19px; border: 1px solid #D8DBE0; background: #fff; color: var(--icon);
  font-size: 20px; font-weight: 700; cursor: pointer; padding: 0; line-height: 1; }

/* ---- Bảng ---- */
#boardhost { position: relative; display: flex; align-items: center; justify-content: center; min-height: 0; min-width: 0; }
#frame { position: relative; background: var(--frame); border: 2px solid var(--frame); border-radius: 6px;
  width: var(--side, 300px); height: var(--side, 300px); transform: scale(var(--zoom, 1)); transform-origin: center; }
#board { display: grid; grid-template-columns: repeat(9, 1fr); grid-template-rows: repeat(9, 1fr); width: 100%; height: 100%; }
.cell { position: relative; background: var(--cell); margin: .5px; display: flex; align-items: center; justify-content: center;
  cursor: pointer; user-select: none; overflow: hidden; }
.cell.bl { margin-left: 2px; }  .cell.bt { margin-top: 2px; }
.cell.br { margin-right: 2px; } .cell.bb { margin-bottom: 2px; }
.cell.peer { background: var(--cell-peer); }
.cell.same { background: var(--cell-same); }
.cell.selected { background: var(--cell-selected); }
.cell .val { font-size: calc(var(--side, 300px) / 12.5); line-height: 1; }
.cell .val.given { color: var(--given); font-weight: 700; }
.cell .val.user { color: var(--user); }
.cell .val.error { color: var(--error); }
.cell .notes { position: absolute; inset: 1px; display: grid; grid-template-columns: repeat(3, 1fr); grid-template-rows: repeat(3, 1fr); }
.cell .notes span { margin: 1px; border-radius: 3px; display: flex; align-items: center; justify-content: center;
  font-size: calc(var(--side, 300px) / 34); color: var(--note); line-height: 1; }
.cell .notes span.hl { background: var(--note-hl); color: var(--accent); font-weight: 700; }
.cell.shake { animation: shake .3s; }
@keyframes shake { 0%,100%{transform:translateX(0)} 20%{transform:translateX(-8px)} 40%{transform:translateX(8px)} 60%{transform:translateX(-6px)} 80%{transform:translateX(6px)} }

#pauseoverlay { position: absolute; inset: 0; background: #F2F3F7; border-radius: 4px; display: none; flex-direction: column;
  align-items: center; justify-content: center; gap: 8px; cursor: pointer; }
#pauseoverlay .big { font-size: 44px; color: var(--muted); }
#pauseoverlay .txt { font-size: 18px; color: var(--text2); }
#frame.paused #pauseoverlay { display: flex; }
#frame.paused #board { visibility: hidden; }

/* ---- Thanh hành động ---- */
#actions { display: grid; grid-template-columns: repeat(5, 1fr); column-gap: 4px; }
.action { position: relative; height: 60px; border-radius: 12px; background: var(--card); border: 0; display: flex; flex-direction: column;
  align-items: center; justify-content: center; gap: 2px; cursor: pointer; padding: 8px 0; }
.action .ic { font-size: 22px; color: var(--icon); line-height: 1; }
.action .cap { font-size: 11px; color: var(--muted); }
.action .badge { position: absolute; top: 4px; right: 6px; font-size: 10px; font-weight: 700; color: #fff; background: var(--accent);
  padding: 1px 5px; border-radius: 4px; }
.action .badge.off { display: none; }
.action .state { position: absolute; top: 4px; right: 6px; font-size: 10px; font-weight: 700; color: var(--muted); }
.action.on { background: #E8EEFB; }
.action.on .state { color: #fff; background: var(--accent); padding: 1px 5px; border-radius: 4px; }

/* ---- Bàn phím số ---- */
#pad { display: grid; grid-template-columns: repeat(9, 1fr); column-gap: 6px; }
.key { height: 56px; border-radius: 10px; background: var(--card); border: 0; display: flex; flex-direction: column; align-items: center;
  justify-content: center; cursor: pointer; padding: 6px 0; }
.key .d { font-size: 24px; font-weight: 700; color: var(--accent); line-height: 1; }
.key .n { font-size: 11px; color: var(--muted); }
.key.dim { opacity: .35; }
.key.dim2 { opacity: .3; }

/* ---- Toast ---- */
#toast { position: fixed; left: 50%; bottom: 90px; transform: translateX(-50%); background: var(--text); color: #fff;
  font-size: 14px; font-weight: 700; padding: 11px 18px; border-radius: 22px; opacity: 0; pointer-events: none; transition: opacity .2s; z-index: 50; }
#toast.show { opacity: 1; }

/* ---- Hộp thoại (không dùng alert/confirm) ---- */
#modal { position: fixed; inset: 0; background: rgba(0,0,0,.35); display: none; align-items: center; justify-content: center; z-index: 100; padding: 16px; }
#modal.show { display: flex; }
#modal .box { background: #fff; border-radius: 16px; padding: 20px; width: min(360px, 100%); box-shadow: 0 10px 40px rgba(0,0,0,.2); }
#modal h3 { margin: 0 0 8px; font-size: 18px; color: var(--text); }
#modal p { white-space: pre-line; margin: 0 0 16px; font-size: 14px; color: var(--text2); line-height: 1.5; }
#modal .btns { display: flex; flex-direction: column; gap: 8px; }
#modal button { border: 0; border-radius: 10px; padding: 11px; font-size: 15px; cursor: pointer; background: #EEF1F6; color: var(--text); }
#modal button.primary { background: var(--accent); color: #fff; font-weight: 700; }

#busy { position: fixed; inset: 0; display: none; align-items: center; justify-content: center; background: rgba(238,241,246,.8); z-index: 90;
  font-size: 16px; color: var(--text2); }
#busy.show { display: flex; }
