/* Hidden attribute fix */
[hidden]{ display: none !important; }

/* Modern mobile-first Minesweeper UI + World Map */
:root{
  --bg: #0b1220;
  --panel: rgba(255,255,255,.06);
  --panel2: rgba(255,255,255,.08);
  --text: rgba(255,255,255,.92);
  --muted: rgba(255,255,255,.64);
  --line: rgba(255,255,255,.12);
  --shadow: 0 18px 60px rgba(0,0,0,.35);
  --cell: rgba(255,255,255,.08);
  --danger: #ff4d4d;
  --ok: #3ddc97;
  --accent: #6ea8ff;
  --radius: 18px;
  --cellSize: 30px;
  --gap: 5px;
  color-scheme: dark;
}

:root.light{
  --bg: #f5f7fb;
  --panel: rgba(0,0,0,.05);
  --panel2: rgba(0,0,0,.06);
  --text: rgba(0,0,0,.88);
  --muted: rgba(0,0,0,.55);
  --line: rgba(0,0,0,.12);
  --shadow: 0 18px 60px rgba(0,0,0,.12);
  --cell: rgba(0,0,0,.06);
  color-scheme: light;
}

*{ box-sizing:border-box; }
html,body{ height:100%; }
body{
  margin:0;
  font-family: ui-sans-serif, system-ui, -apple-system, Segoe UI, Roboto, Helvetica, Arial;
  background: radial-gradient(1000px 600px at 20% 0%, rgba(110,168,255,.18), transparent 55%),
              radial-gradient(900px 500px at 90% 20%, rgba(61,220,151,.14), transparent 55%),
              var(--bg);
  color: var(--text);
}

.topbar{
  position: sticky;
  top: 0;
  z-index: 5;
  backdrop-filter: blur(14px);
  background: linear-gradient(to bottom, rgba(0,0,0,.22), rgba(0,0,0,0));
  padding: calc(10px + env(safe-area-inset-top)) 14px 10px 14px;
  display:flex;
  align-items:center;
  justify-content:space-between;
  gap: 12px;
  border-bottom: 1px solid var(--line);
}

.brand{ display:flex; align-items:center; gap:10px; }
.logo{
  width:42px; height:42px;
  border-radius: 14px;
  background: var(--panel);
  display:grid; place-items:center;
  box-shadow: var(--shadow);
}
.title{ font-weight:700; letter-spacing:.2px; }
.subtitle{ font-size:12px; color:var(--muted); margin-top:2px; }

.hud{ display:flex; align-items:center; gap:10px; }
.pill{
  min-width: 74px;
  padding: 10px 12px;
  border-radius: 999px;
  background: var(--panel);
  border: 1px solid var(--line);
  display:flex;
  align-items:center;
  justify-content:space-between;
  gap: 10px;
}
.pill.small{ min-width: 0; padding: 8px 10px; }
.pillLabel{ opacity:.9; }
.pillValue{ font-variant-numeric: tabular-nums; font-weight:800; }

.face{
  width:48px; height:48px;
  border-radius: 16px;
  border: 1px solid var(--line);
  background: var(--panel2);
  box-shadow: var(--shadow);
  font-size: 22px;
  cursor:pointer;
  transition: transform .12s ease;
}
.face:active{ transform: scale(.98); }

.actions{ display:flex; gap:10px; align-items:center; }
.iconBtn{
  width:44px; height:44px;
  border-radius: 14px;
  border: 1px solid var(--line);
  background: var(--panel);
  color: var(--text);
  cursor:pointer;
  font-size:18px;
}

.wrap{
  padding: 14px;
  display:flex;
  justify-content:center;
}

.panel{
  width: min(980px, 100%);
  border-radius: var(--radius);
  background: rgba(255,255,255,.04);
  border: 1px solid var(--line);
  box-shadow: var(--shadow);
  padding: 14px;
}

.row{
  display:flex;
  align-items:center;
  justify-content:space-between;
  gap: 12px;
  flex-wrap: wrap;
}

.h1{ font-size: 18px; font-weight: 900; }
.h2{ font-size: 14px; font-weight: 800; }
.hint{ font-size: 12px; color: var(--muted); }

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

.divider{
  height: 1px;
  background: var(--line);
  margin: 14px 0;
}

/* World Grid */
.worldGrid{
  margin-top: 14px;
  display:grid;
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
  gap: 12px;
}

.levelCard{
  border-radius: 18px;
  border: 1px solid var(--line);
  background: rgba(255,255,255,.04);
  padding: 14px;
  cursor:pointer;
  position: relative;
  overflow:hidden;
  transition: transform .12s ease;
}
.levelCard:active{ transform: scale(.99); }

.levelTop{ display:flex; align-items:flex-start; justify-content:space-between; gap:10px; }
.levelTitle{ font-weight: 900; }
.levelSub{ font-size: 12px; color: var(--muted); margin-top: 4px; }
.levelTags{ display:flex; gap:8px; flex-wrap: wrap; margin-top: 10px; }
.tag{
  font-size: 12px;
  border: 1px solid var(--line);
  background: rgba(255,255,255,.05);
  padding: 6px 10px;
  border-radius: 999px;
}
.lock{
  position:absolute;
  inset:0;
  background: rgba(0,0,0,.35);
  display:grid;
  place-items:center;
  font-weight: 900;
}
:root.light .lock{ background: rgba(255,255,255,.45); }

.levelCard.locked{ cursor:not-allowed; }
.levelCard.locked:active{ transform:none; }

.badge{
  border: 1px solid rgba(110,168,255,.35);
  background: rgba(110,168,255,.18);
  padding: 8px 10px;
  border-radius: 999px;
  font-size: 12px;
  font-weight: 800;
  white-space: nowrap;
}

/* Seg */
.seg{
  display:flex;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 999px;
  padding: 4px;
  gap:4px;
  width: fit-content;
}
.segBtn{
  border:0;
  background: transparent;
  color: var(--muted);
  padding: 10px 12px;
  border-radius: 999px;
  cursor:pointer;
}
.segBtn.active{
  background: rgba(110,168,255,.22);
  color: var(--text);
  border: 1px solid rgba(110,168,255,.28);
}

.custom{
  margin-top: 12px;
  padding: 12px;
  border-radius: 16px;
  background: var(--panel);
  border: 1px solid var(--line);
  display:flex;
  gap: 10px;
  flex-wrap: wrap;
  align-items: end;
  width: fit-content;
}
.field{ display:flex; flex-direction:column; gap:6px; }
.field label{ font-size:12px; color: var(--muted); }
.field input{
  width: 110px;
  padding: 10px 10px;
  border-radius: 12px;
  border: 1px solid var(--line);
  background: rgba(255,255,255,.06);
  color: var(--text);
  outline:none;
}

.btn{
  padding: 10px 14px;
  border-radius: 14px;
  border: 1px solid rgba(110,168,255,.32);
  background: rgba(110,168,255,.22);
  color: var(--text);
  cursor:pointer;
}
.btn.ghost{
  border: 1px solid var(--line);
  background: rgba(255,255,255,.06);
  color: var(--text);
}

.boardShell{
  margin-top: 14px;
  padding: 14px;
  border-radius: 18px;
  background: var(--panel);
  border: 1px solid var(--line);
  overflow: hidden;
  -webkit-overflow-scrolling: touch;
}

.board{
  display:grid;
  gap: var(--gap);
  width: fit-content;
  margin: 0 auto;
  touch-action: manipulation;
  user-select: none;
}

.cell{
  width: var(--cellSize);
  height: var(--cellSize);
  border-radius: 12px;
  border: 1px solid var(--line);
  background: var(--cell);
  display:grid;
  place-items:center;
  font-weight: 800;
  font-variant-numeric: tabular-nums;
  cursor:pointer;
  transition: transform .08s ease, background .12s ease;
  position: relative;
}
.cell:active{ transform: scale(.98); }

.cell.revealed{
  background: rgba(255,255,255,.03);
  border-color: rgba(255,255,255,.10);
  cursor: default;
}
:root.light .cell.revealed{
  background: rgba(0,0,0,.03);
  border-color: rgba(0,0,0,.10);
}

.cell.flagged::after{
  content:"🚩";
  font-size: 16px;
}
.cell.mine.revealed{
  background: rgba(255,77,77,.18);
  border-color: rgba(255,77,77,.35);
}
.cell.mine.revealed::after{
  content:"💥";
  font-size: 16px;
}

.n1{ color: #6ea8ff; }
.n2{ color: #3ddc97; }
.n3{ color: #ffb74d; }
.n4{ color: #b28dff; }
.n5{ color: #ff6ea8; }
.n6{ color: #6effe0; }
.n7{ color: rgba(255,255,255,.85); }
.n8{ color: rgba(255,255,255,.7); }

.footerRow{
  margin-top: 14px;
  display:flex;
  gap:10px;
  align-items:center;
  flex-wrap: wrap;
  justify-content: space-between;
}
.stat{
  min-width: 90px;
  padding: 10px 12px;
  border-radius: 16px;
  border: 1px solid var(--line);
  background: rgba(255,255,255,.04);
}
.statLabel{ font-size: 12px; color: var(--muted); }
.statValue{ font-weight: 800; margin-top: 2px; font-variant-numeric: tabular-nums; }

/* Modal */
.modalBackdrop{
  position: fixed; inset:0;
  background: rgba(0,0,0,.45);
  backdrop-filter: blur(8px);
  z-index: 20;
}
.modal{
  position: fixed;
  left: 50%; top: 50%;
  transform: translate(-50%, -50%);
  width: min(520px, calc(100vw - 26px));
  border-radius: 18px;
  border: 1px solid var(--line);
  background: rgba(15,20,34,.92);
  box-shadow: var(--shadow);
  z-index: 21;
}
:root.light .modal{
  background: rgba(255,255,255,.92);
}
.modalHeader{
  padding: 14px;
  display:flex;
  align-items:center;
  justify-content:space-between;
  border-bottom: 1px solid var(--line);
}
.modalTitle{ font-weight: 800; }
.modalBody{ padding: 14px; display:flex; flex-direction:column; gap:14px; }
.modalFooter{
  padding: 14px;
  border-top: 1px solid var(--line);
  display:flex;
  justify-content: space-between;
  gap:10px;
}

.toggleRow{
  display:flex;
  align-items:center;
  justify-content:space-between;
  gap: 12px;
  padding: 12px;
  border-radius: 16px;
  border: 1px solid var(--line);
  background: rgba(255,255,255,.04);
}
.toggleTitle{ font-weight: 700; }
.toggleDesc{ font-size: 12px; color: var(--muted); margin-top: 4px; }
.select{
  padding: 10px 10px;
  border-radius: 12px;
  border: 1px solid var(--line);
  background: rgba(255,255,255,.06);
  color: var(--text);
}

/* Switch */
.switch{ position: relative; display:inline-block; width: 52px; height: 30px; }
.switch input{ display:none; }
.slider{
  position:absolute; inset:0;
  background: rgba(255,255,255,.10);
  border: 1px solid var(--line);
  border-radius: 999px;
  cursor:pointer;
}
.slider:before{
  content:"";
  position:absolute; height:24px; width:24px;
  left: 3px; top: 50%;
  transform: translateY(-50%);
  border-radius: 999px;
  background: rgba(255,255,255,.9);
  transition: transform .15s ease;
}
.switch input:checked + .slider{
  background: rgba(110,168,255,.24);
  border-color: rgba(110,168,255,.32);
}
.switch input:checked + .slider:before{
  transform: translate(22px, -50%);
}

@media (max-width: 480px){
  :root{ --cellSize: 32px; --gap: 5px; }
  .pill{ min-width: 66px; }
}


/* PWA UX locks: no selection, no iOS callout, no highlight */
html, body, .panel, .boardShell, .board, .cell, button, .topbar, .hud, .pill, .levelCard, .tag, .badge {
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
}

input, textarea, select {
  -webkit-user-select: text;
  user-select: text;
  -webkit-touch-callout: default;
}

/* Avoid page scrolling in-game */
body {
  overscroll-behavior: none;
}
body.noscroll {
  overflow: hidden;
  position: fixed;
  width: 100%;
}


/* Topbar responsive: avoid overflow on iPhone */
.topbar{
  flex-wrap: wrap;
}

.brand{
  flex: 1 1 auto;
  min-width: 180px;
}

.hud{
  flex: 1 1 260px;
  justify-content: center;
  flex-wrap: wrap;
}

.actions{
  flex: 0 0 auto;
}

/* Tighten on small phones */
@media (max-width: 430px){
  .boardShell{ padding: 10px; }
  .topbar{
    padding-left: 10px;
    padding-right: 10px;
    gap: 8px;
  }
  .logo{
    width: 36px;
    height: 36px;
    border-radius: 12px;
  }
  .subtitle{ display:none; }
  .pill{
    min-width: 60px;
    padding: 8px 10px;
    gap: 8px;
  }
  .face{
    width: 44px;
    height: 44px;
    border-radius: 14px;
    font-size: 20px;
  }
  .iconBtn{
    width: 40px;
    height: 40px;
    border-radius: 13px;
    font-size: 16px;
  }
}

/* Ultra small */
@media (max-width: 360px){
  .brand{ min-width: 150px; }
  .pill{ min-width: 54px; padding: 7px 9px; }
  .pillValue{ font-size: 14px; }
}


/* Help modal content */
.helpBlock{
  padding: 12px;
  border-radius: 16px;
  border: 1px solid var(--line);
  background: rgba(255,255,255,.04);
}
.helpTitle{
  font-weight: 800;
  margin-bottom: 6px;
}
.helpText{
  font-size: 13px;
  color: var(--text);
  line-height: 1.35;
}
.helpText b{ font-weight: 900; }
.helpList{
  margin: 8px 0 0 18px;
  padding: 0;
  color: var(--text);
}
.helpList li{
  margin: 6px 0;
}



#helpModal .modalBody{
  overflow: auto;
  -webkit-overflow-scrolling: touch;
  max-height: calc(100vh - 180px);
}
#helpModal .modalFooter{
  position: sticky;
  bottom: 0;
  background: inherit;
  padding-bottom: calc(14px + env(safe-area-inset-bottom));
}



#helpBackdrop{
  z-index: 1000;
}


/* Ensure modal always centered and above fixed body */

/* Footer always clickable */
#helpModal .modalFooter{
  position: sticky;
  bottom: 0;
  background: inherit;
  z-index: 2;
}

/* Make sure modal content scrolls independently */
#helpModal .modalBody{
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
}



#helpBackdrop{
  z-index: 9998 !important;
  pointer-events: auto;
}

/* Scroll inside body, reserve space for footer */
#helpModal .modalBody{
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  padding-bottom: calc(74px + env(safe-area-inset-bottom));
}

/* Footer pinned inside modal (not sticky) */
#helpModal .modalFooter{
  position: absolute;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 2;
  background: inherit;
  border-top: 1px solid var(--line);
  padding: 14px;
  padding-bottom: calc(14px + env(safe-area-inset-bottom));
}

/* Make sure header stays on top */
#helpModal .modalHeader{
  position: relative;
  z-index: 3;
}



#helpBackdrop{
  z-index: 99998 !important;
  pointer-events: auto !important;
}

#helpModal .modalHeader{
  position: sticky;
  top: 0;
  z-index: 5;
  background: inherit;
}

#helpModal .modalBody{
  flex: 1 1 auto;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  padding-bottom: calc(86px + env(safe-area-inset-bottom));
}

#helpModal .modalFooter{
  position: sticky;
  bottom: 0;
  z-index: 6;
  background: inherit;
  border-top: 1px solid var(--line);
  padding: 14px;
  padding-bottom: calc(14px + env(safe-area-inset-bottom));
}

#helpModal .modalFooter .btn{
  width: 100%;
  justify-content: center;
  font-weight: 900;
}

/* Ensure taps are not eaten by anything inside help modal */
#helpModal, #helpModal *{
  -webkit-touch-callout: none;
  -webkit-user-select: none;
  user-select: none;
}
#helpModal button{
  pointer-events: auto !important;
  touch-action: manipulation !important;
}


/* Help modal: clean + iOS-safe */
.helpModal{
  max-height: calc(100vh - 24px);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}
.helpModal .modalBody{
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  padding-bottom: calc(74px + env(safe-area-inset-bottom));
}
.helpModal .modalFooter{
  border-top: 1px solid var(--line);
  background: inherit;
  padding-bottom: calc(14px + env(safe-area-inset-bottom));
}
