/* ==========================================================================
   KOME DESIGN V1 — NONOGRAM STYLESHEET
   Ngôn ngữ thiết kế: "Hình khối cơ bản, màu nguyên bản, lõi đặc, rìa kính."
   ========================================================================== */

:root {
  /* Kome Design V1 Color Tokens */
  --k-yellow: #FFC90E;
  --k-gold: #FABB05;
  --k-red: #F34040;
  --k-blue: #4488FF;
  --k-blue-600: #2D6BE4;
  --k-red-600: #D92D2D;
  --k-ink: #1A1B2E;
  --k-on-yellow: #1A1B2E;
  --k-bg: #F6F7FB;
  --k-surface: #FFFFFF;
  --k-border: #E3E5EE;

  /* Shape & Metrics */
  --k-r-md: 16px;
  --k-r-lg: 24px;
  --k-spring: cubic-bezier(.34, 1.56, .64, 1);
  --k-ease: cubic-bezier(.2, 0, 0, 1);
  --k-font: "Be Vietnam Pro", Inter, system-ui, -apple-system, sans-serif;
  --k-shadow: 0 1px 2px rgba(26, 27, 46, .08), 0 4px 12px rgba(26, 27, 46, .06);
  --k-glass-bg: color-mix(in srgb, var(--k-surface) 72%, transparent);
  --k-glass-border: color-mix(in srgb, #fff 55%, transparent);

  /* Canvas 3D Engine Variables (readColors() in nonogram.js) */
  --bg: var(--k-bg);
  --card: #FFFFFF;
  --slab: #D6DAEA;
  --well: #EEF1F8;
  --bar: var(--k-glass-bg);
  --ink: #1A1B2E;
  --line: #CAD0E2;
  --fill: #1A1B2E;
  --x: #D92D2D;
  --hl: rgba(68, 136, 255, 0.16);
  --btn: var(--k-surface);
  --on: var(--k-yellow);
  --onink: var(--k-on-yellow);
  color-scheme: light;
}

:root[data-theme="dark"] {
  --k-bg: #14151F;
  --k-surface: #1E2030;
  --k-border: #33364A;
  --k-ink: #F3F4FA;
  --k-shadow: 0 1px 2px rgba(0, 0, 0, .4);
  --k-glass-bg: color-mix(in srgb, var(--k-surface) 75%, transparent);
  --k-glass-border: color-mix(in srgb, #fff 15%, transparent);

  /* Canvas 3D Engine Variables (Dark mode) */
  --bg: var(--k-bg);
  --card: #1E2030;
  --slab: #0E0F17;
  --well: #14151F;
  --bar: var(--k-glass-bg);
  --ink: #F3F4FA;
  --line: #33364A;
  --fill: #FFC90E; /* Ô tô màu vàng Kome nổi bật trên bảng tối */
  --x: #F34040;
  --hl: rgba(68, 136, 255, 0.25);
  --btn: #272A3E;
  --on: var(--k-yellow);
  --onink: var(--k-on-yellow);
  color-scheme: dark;
}

/* Base Reset */
*, *::before, *::after {
  box-sizing: border-box;
}

html, body {
  height: 100%;
  margin: 0;
  padding: 0;
}

body {
  position: relative;
  display: flex;
  flex-direction: column;
  height: 100dvh;
  background: var(--bg);
  color: var(--k-ink);
  font: 400 14px/1.4 var(--k-font);
  overflow: hidden;
  user-select: none;
  -webkit-user-select: none;
}

/* Accessibility Focus Visible */
:focus-visible {
  outline: 3px solid var(--k-blue);
  outline-offset: 2px;
}

/* Kome Glass Surface Helper */
.glass {
  background: var(--k-glass-bg);
  -webkit-backdrop-filter: blur(24px) saturate(160%);
  backdrop-filter: blur(24px) saturate(160%);
  border: 1px solid var(--k-glass-border);
}

@supports not (backdrop-filter: blur(1px)) {
  .glass {
    background: var(--k-surface);
  }
}

@media (prefers-reduced-transparency: reduce) {
  .glass {
    background: var(--k-surface) !important;
    backdrop-filter: none !important;
    -webkit-backdrop-filter: none !important;
  }
}

/* ==========================================================================
   HEADER / MENU (FLOAT PHÍA TRÊN BÊN TRÁI, 3 HÀNG GỌN GÀNG)
   ========================================================================== */
#menu {
  position: relative;
  z-index: 20;
  align-self: flex-start;
  margin: 8px 10px 4px 10px;
  display: flex;
  flex-direction: column;
  gap: 6px;
  padding: 7px 9px;
  width: fit-content;
  max-width: calc(100vw - 20px);
  border-radius: var(--k-r-lg);
  box-shadow: 0 4px 16px rgba(26, 27, 46, 0.06);
  flex: none;
  pointer-events: auto;
}

.row {
  display: flex;
  align-items: center;
  justify-content: flex-start;
  gap: 6px;
  flex-wrap: nowrap;
  overflow-x: auto;
  scrollbar-width: none;
  -webkit-overflow-scrolling: touch;
}

.row::-webkit-scrollbar {
  display: none;
}

.brand {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  text-decoration: none;
  color: inherit;
  flex: none;
  padding-right: 2px;
}

.logo {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  transition: transform .2s var(--k-spring);
}

.logo:hover {
  transform: rotate(-8deg) scale(1.08);
}

.title {
  font-size: 15px;
  font-weight: 800;
  letter-spacing: -0.02em;
  color: var(--k-ink);
}

/* ==========================================================================
   SEGMENTED CONTROLS & PILLS (.seg)
   ========================================================================== */
.seg {
  display: inline-flex;
  align-items: center;
  padding: 2px;
  border-radius: 999px;
  background: var(--k-border);
  gap: 2px;
  flex: none;
}

.seg button {
  font: 600 12.5px var(--k-font);
  border: 0;
  background: transparent;
  color: var(--k-ink);
  min-height: 28px;
  padding: 0 9px;
  border-radius: 999px;
  cursor: pointer;
  transition: background .18s var(--k-ease), transform .15s var(--k-spring), color .18s;
  white-space: nowrap;
}

.seg button:hover:not(.on) {
  background: color-mix(in srgb, var(--k-surface) 65%, transparent);
}

.seg button.on {
  background: var(--k-yellow);
  color: var(--k-on-yellow);
  font-weight: 700;
  box-shadow: 0 2px 6px rgba(255, 201, 14, 0.4);
}

.seg button:active {
  transform: scale(.94);
}

/* ==========================================================================
   CHIP BUTTONS & SPECIAL ACTIVE STATES
   ========================================================================== */
.chip-btn {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  height: 30px;
  min-height: 30px;
  padding: 0 10px;
  border-radius: 999px;
  border: 1.5px solid var(--k-border);
  background: var(--k-surface);
  color: var(--k-ink);
  font: 600 12.5px var(--k-font);
  cursor: pointer;
  white-space: nowrap;
  flex: none;
  transition: transform .15s var(--k-spring), background .18s, border-color .18s, color .18s, box-shadow .18s;
}

.chip-btn:hover {
  border-color: var(--k-blue);
  background: color-mix(in srgb, var(--k-blue) 8%, var(--k-surface));
}

.chip-btn:active {
  transform: scale(.95);
}

#color.on,
#master.on {
  background: var(--k-blue-600);
  border-color: var(--k-blue-600);
  color: #FFFFFF;
  box-shadow: 0 3px 10px rgba(45, 107, 228, 0.35);
  font-weight: 700;
}

#color.on:hover,
#master.on:hover {
  background: #2559c2;
}

/* ==========================================================================
   ICON BUTTONS (.ib)
   ========================================================================== */
.ib {
  width: 32px;
  height: 32px;
  min-height: 32px;
  padding: 0;
  border-radius: 50%;
  border: 1.5px solid var(--k-border);
  background: var(--k-surface);
  color: var(--k-ink);
  font-size: 14px;
  font-weight: 700;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  transition: transform .15s var(--k-spring), background .18s, border-color .18s;
  flex: none;
}

.ib:hover {
  border-color: var(--k-blue);
  background: color-mix(in srgb, var(--k-blue) 8%, var(--k-surface));
}

.ib:active {
  transform: scale(.92);
}

/* Time Badge */
.time-badge {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 58px;
  height: 32px;
  padding: 0 8px;
  border-radius: 999px;
  background: var(--k-surface);
  border: 1.5px solid var(--k-border);
  font-size: 14px;
  font-weight: 800;
  font-variant-numeric: tabular-nums;
  color: var(--k-ink);
  letter-spacing: 0.04em;
  box-shadow: inset 0 1px 2px rgba(0, 0, 0, 0.04);
  flex: none;
}

/* ==========================================================================
   MAIN CANVAS WRAPPER (KHÔNG ĐÈ NHAU VỚI MENU VÀ FOOTER)
   ========================================================================== */
#wrap {
  flex: 1;
  min-height: 0;
  display: flex;
  align-items: flex-start;
  padding: 6px 12px;
  overflow: auto;
  scrollbar-width: none;
}

#wrap::-webkit-scrollbar {
  display: none;
}

/* margin:auto = căn giữa khi bàn vừa khung; khi bàn lớn hơn khung thì tự về 0 để cuộn/kéo tới được mọi mép (align/justify-content làm mất mép trên/trái) */
#cv {
  touch-action: none;
  flex: none;
  max-width: none;
  margin: auto;
}

/* ==========================================================================
   FOOTER BAR (#foot - CHỨA NÚT CHUYỂN ĐỔI FILL/CROSS)
   ========================================================================== */
#foot {
  position: relative;
  z-index: 20;
  align-self: flex-start;
  margin: 4px 10px calc(8px + env(safe-area-inset-bottom)) 10px;
  height: 48px;
  width: fit-content;
  max-width: calc(100vw - 20px);
  border-radius: 999px;
  padding: 0 12px 0 0;
  display: inline-flex;
  align-items: center;
  gap: 8px;
  flex-wrap: nowrap;
  box-shadow: 0 4px 16px rgba(26, 27, 46, 0.06);
  flex: none;
}

.foot-main {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  flex: none;
}

.subbar {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  flex: none;
}

.msg-badge {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  font-weight: 600;
  font-size: 12.5px;
  color: var(--k-ink);
  background: color-mix(in srgb, var(--k-border) 45%, transparent);
  padding: 4px 10px;
  border-radius: 999px;
  white-space: nowrap;
}

.palette {
  display: flex;
  align-items: center;
  gap: 6px;
}

.palette[hidden] {
  display: none;
}

.sw {
  width: 28px;
  height: 28px;
  min-height: 0;
  padding: 0;
  border-radius: 50%;
  border: 2px solid rgba(0, 0, 0, 0.15);
  box-shadow: 0 2px 6px rgba(0, 0, 0, 0.12), inset 0 2px 0 rgba(255, 255, 255, 0.4);
  cursor: pointer;
  transition: transform .15s var(--k-spring), outline .15s;
}

.sw:hover {
  transform: scale(1.1);
}

.sw.on {
  outline: 3px solid var(--k-ink);
  outline-offset: 2px;
  transform: scale(1.15);
}

/* Standard Buttons (.btn) */
.btn {
  font: 600 13px var(--k-font);
  min-height: 32px;
  padding: 0 12px;
  border-radius: var(--k-r-md);
  border: 1.5px solid transparent;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  gap: 4px;
  white-space: nowrap;
  transition: transform .15s var(--k-spring), background .18s var(--k-ease), border-color .18s;
}

.btn:active {
  transform: scale(.96);
}

/* Primary Button (Yellow) — Next Puzzle: chỉ hiện khi kết thúc màn chơi */
.b-p {
  background: var(--k-yellow);
  color: var(--k-on-yellow);
  box-shadow: 0 3px 10px rgba(255, 201, 14, 0.4);
  font-weight: 700;
  border: 0;
}

.b-p:hover {
  background: var(--k-gold);
}

#next[hidden] {
  display: none !important;
}

/* Khi Next Puzzle hiện ra thì ẩn các nút màu đi */
#next:not([hidden]) ~ #pal {
  display: none !important;
}

/* Share Button (Màu xanh Kome Blue 600 giống nút color) */
.b-s {
  background: var(--k-blue-600);
  border: 1.5px solid var(--k-blue-600);
  color: #FFFFFF;
  box-shadow: 0 2px 8px rgba(45, 107, 228, 0.35);
  font-weight: 600;
}

.b-s:hover {
  background: #2559c2;
}

/* ==========================================================================
   NÚT TRÒN FILL/CROSS TRONG KHUNG CHÂN TRANG (#mode .fab)
   (Đường kính bằng chiều cao khung chân trang = 48px, to nhất có thể)
   ========================================================================== */
.fab {
  position: relative;
  flex: none;
  width: 48px;
  height: 48px;
  min-height: 48px;
  padding: 0;
  padding-bottom: 3px;
  border: 0;
  border-radius: 50%;
  font-size: 24px;
  line-height: 1;
  font-weight: 700;
  background: var(--k-yellow);
  color: var(--k-on-yellow);
  box-shadow: 0 2px 8px rgba(255, 201, 14, 0.45);
  cursor: pointer;
  display: grid;
  place-items: center;
  text-align: center;
  transition: transform .18s var(--k-spring), background .18s var(--k-ease), box-shadow .18s;
}

.fab.cross {
  background: var(--k-red-600);
  color: #FFFFFF;
  box-shadow: 0 2px 8px rgba(217, 45, 45, 0.45);
}

.fab:active {
  transform: scale(.92);
}

/* ==========================================================================
   ZOOM BUTTONS (#zoom - FLOAT Ở GIỮA MÀN BÊN TRÁI)
   ========================================================================== */
#zoom {
  position: fixed;
  left: 10px;
  top: 50%;
  transform: translateY(-50%);
  z-index: 35;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

#zoom[hidden] {
  display: none !important;
}

#zoom button {
  width: 38px;
  height: 38px;
  min-height: 0;
  padding: 0;
  border-radius: 50%;
  border: 1.5px solid var(--k-border);
  background: var(--k-surface);
  color: var(--k-ink);
  font-size: 20px;
  font-weight: 700;
  box-shadow: 0 4px 14px rgba(0, 0, 0, 0.12);
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: transform .15s var(--k-spring), background .15s;
}

#zoom button:active {
  transform: scale(.92);
}

#pan-btn.on {
  background: var(--k-yellow);
  border-color: var(--k-gold);
  color: var(--k-on-yellow);
  box-shadow: 0 2px 10px rgba(255, 201, 14, 0.45);
}

/* ==========================================================================
   MÀN HÌNH THẬT NHỎ (< 460px, không đủ chỗ trên 1 dòng):
   Tách 2 dòng: dòng trên (Next Puzzle / các ô màu) float bên phải,
   dòng dưới (#mode, #share, #msg) float bên trái.
   Bàn game tự co lại theo layout, không bao giờ bị chèn lên!
   ========================================================================== */
@media (max-width: 460px) {
  #foot {
    display: flex;
    flex-direction: column-reverse; /* dòng trên là subbar, dòng dưới là foot-main */
    align-items: stretch;
    height: auto;
    background: transparent;
    border: 0;
    box-shadow: none;
    padding: 0;
    width: calc(100% - 20px);
    margin: 4px 10px calc(8px + env(safe-area-inset-bottom)) 10px;
    gap: 6px;
  }

  .foot-main {
    align-self: flex-start;
    height: 48px;
    border-radius: 999px;
    padding: 0 12px 0 0;
    background: var(--k-glass-bg);
    -webkit-backdrop-filter: blur(24px) saturate(160%);
    backdrop-filter: blur(24px) saturate(160%);
    border: 1px solid var(--k-glass-border);
    box-shadow: 0 4px 16px rgba(26, 27, 46, 0.06);
  }

  .subbar {
    align-self: flex-end; /* float bên phải */
    border-radius: 999px;
    padding: 4px 8px;
    background: var(--k-glass-bg);
    -webkit-backdrop-filter: blur(24px) saturate(160%);
    backdrop-filter: blur(24px) saturate(160%);
    border: 1px solid var(--k-glass-border);
    box-shadow: 0 4px 16px rgba(26, 27, 46, 0.06);
  }

  .subbar:has(#next[hidden]):has(#pal[hidden]),
  .subbar:has(#next[hidden]):not(:has(#pal)),
  .subbar:has(#pal[hidden]):not(:has(#next)) {
    display: none !important;
  }
}

/* ==========================================================================
   DESKTOP / MÀN HÌNH RỘNG: TÁCH RIÊNG CỘT MENU/FOOT VÀ CỘT GAME
   Đảm bảo 100% không bao giờ đè lên nhau trên bất kỳ độ phân giải nào
   ========================================================================== */
@media (min-width: 820px) and (min-aspect-ratio: 1/1) {
  body {
    display: grid;
    grid-template-columns: auto 1fr;
    grid-template-rows: 1fr auto;
    grid-template-areas:
      "menu game"
      "foot game";
    height: 100dvh;
  }

  #menu {
    grid-area: menu;
    align-self: start;
    margin: 10px 0 0 10px;
  }

  #foot {
    grid-area: foot;
    align-self: end;
    margin: 0 0 calc(10px + env(safe-area-inset-bottom)) 10px;
  }

  #wrap {
    grid-area: game;
    height: 100%;
    width: 100%;
    padding: 10px 16px;
  }

  #cv {
    margin: auto 0 auto auto; /* căn sát phải khi vừa khung */
  }

  #zoom {
    position: fixed;
    left: 12px;
    top: 50%;
    transform: translateY(-50%);
  }
}

/* ==========================================================================
   PRINT STYLES
   ========================================================================== */
#plink {
  display: none;
}

@page {
  margin: 12mm;
}

@media print {
  body > *:not(#wrap):not(#plink) {
    display: none !important;
  }
  body {
    display: block;
    height: auto;
    overflow: visible;
    background: #fff;
    color: #000;
  }
  #wrap {
    display: block;
    overflow: visible;
    padding: 0;
    text-align: center;
  }
  #cv {
    width: auto !important;
    height: auto !important;
    max-width: 100%;
    max-height: 235mm;
    margin: auto !important;
  }
  #plink {
    display: block;
    text-align: center;
    margin-top: 8px;
    font: 13px system-ui, sans-serif;
    word-break: break-all;
  }
}

@media (prefers-reduced-motion: reduce) {
  * {
    animation: none !important;
    transition: none !important;
  }
}
