/* Flag Beat Python Lab - styles
   CONTRAST RULE (strict): light background => dark text (#111827). Dark background => white text (#FFFFFF).
   Never grey-on-white, never white-on-light. Mid-tone backgrounds are not used for text. */
:root {
  --ink: #111827; --white: #FFFFFF; --navy: #1E2A78; --purple: #4B2BB5;
  --yellow: #FFD23F; --cyan: #4CC9F0; --pink: #FF8CC6; --green: #5EE6A8; --orange: #FFB347; --lav: #C9B6FF; --cream: #FFF4C2;
  --good: #0B6B3A; --bad: #9B1C31;
  --r: 20px; --bw: 3px;
  --font: ui-rounded, "SF Pro Rounded", "Nunito", system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  --mono: ui-monospace, "SF Mono", Menlo, Consolas, "Courier New", monospace;
}
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; padding: 0; }
body {
  font-family: var(--font); font-size: 20px; font-weight: 700; color: var(--white);
  background: radial-gradient(circle at 20% 10%, #3a3fb0 0, transparent 40%), radial-gradient(circle at 85% 90%, #5a1f9e 0, transparent 40%), linear-gradient(160deg, var(--navy), var(--ink));
  background-attachment: fixed; min-height: 100vh; min-height: 100dvh;
  -webkit-text-size-adjust: 100%; overscroll-behavior-y: none;
}
button, input, select, textarea { font-family: inherit; font-size: inherit; font-weight: 800; }
h1, h2, h3, h4 { margin: 0; line-height: 1.15; font-weight: 900; }
p { margin: 0 0 .6em; }
.hidden { display: none !important; }

/* ---------- layout ---------- */
#app { display: flex; flex-direction: column; min-height: 100vh; min-height: 100dvh; }
#topbar { background: var(--ink); color: var(--white); padding: 8px 14px; display: flex; align-items: center; gap: 12px; position: sticky; top: 0; z-index: 30; border-bottom: var(--bw) solid var(--yellow); }
#topbar .brand { font-size: 22px; font-weight: 900; letter-spacing: .5px; white-space: nowrap; }
#topbar .grow { flex: 1; }
.who { display: flex; align-items: center; gap: 10px; background: var(--white); color: var(--ink); border: var(--bw) solid var(--ink); border-radius: 999px; padding: 4px 14px 4px 6px; min-height: 52px; cursor: pointer; }
.who .av { font-size: 30px; line-height: 1; }
.who .nm { font-weight: 900; font-size: 18px; line-height: 1.1; }
.who .rk { font-size: 14px; font-weight: 800; }
.xpbar { width: 120px; height: 12px; background: var(--white); border: 2px solid var(--ink); border-radius: 99px; overflow: hidden; margin-top: 3px; }
.xpbar > i { display: block; height: 100%; background: var(--green); }
#main { flex: 1; padding: 14px; max-width: 1240px; width: 100%; margin: 0 auto; padding-bottom: 110px; }
#tabbar { position: fixed; left: 0; right: 0; bottom: 0; z-index: 40; background: var(--navy); border-top: var(--bw) solid var(--yellow); display: flex; gap: 6px; padding: 8px 8px calc(8px + env(safe-area-inset-bottom)); overflow-x: auto; }
#tabbar button { flex: 1 0 auto; min-width: 84px; min-height: 64px; border: var(--bw) solid var(--white); background: transparent; color: var(--white); border-radius: 18px; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px; font-size: 14px; font-weight: 900; cursor: pointer; padding: 4px 8px; }
#tabbar button .ic { font-size: 28px; line-height: 1; }
#tabbar button.active { background: var(--yellow); color: var(--ink); border-color: var(--ink); }

/* ---------- missions strip (always visible, top) ---------- */
#missions { background: var(--cream); color: var(--ink); border-bottom: var(--bw) solid var(--ink); }
#missions .row { display: flex; align-items: center; gap: 12px; padding: 8px 14px; min-height: 56px; cursor: pointer; max-width: 1240px; margin: 0 auto; }
#missions .ttl { font-weight: 900; font-size: 17px; white-space: nowrap; }
#missions .bar { flex: 1; height: 18px; background: var(--white); border: 3px solid var(--ink); border-radius: 99px; overflow: hidden; min-width: 60px; }
#missions .bar > i { display: block; height: 100%; background: var(--green); transition: width .4s; }
#missions .cnt { font-weight: 900; white-space: nowrap; }
#missions .next { font-weight: 900; font-size: 15px; background: var(--ink); color: var(--white); border-radius: 999px; padding: 6px 14px; white-space: nowrap; }
#missionPanel { background: var(--white); color: var(--ink); border-top: 3px solid var(--ink); padding: 14px; }
#missionPanel .inner { max-width: 1240px; margin: 0 auto; }
.rules { display: grid; grid-template-columns: repeat(auto-fit, minmax(230px, 1fr)); gap: 8px; margin: 8px 0 12px; }
.rules div { background: var(--yellow); color: var(--ink); border: 3px solid var(--ink); border-radius: 14px; padding: 8px 12px; font-size: 16px; font-weight: 900; }
.quest { display: flex; align-items: center; gap: 12px; background: var(--white); color: var(--ink); border: 3px solid var(--ink); border-radius: 16px; padding: 10px 12px; margin-bottom: 8px; min-height: 64px; }
.quest.done { background: var(--green); }
.quest .box { width: 40px; height: 40px; flex: none; border: 3px solid var(--ink); border-radius: 10px; background: var(--white); display: flex; align-items: center; justify-content: center; font-size: 26px; color: var(--ink); }
.quest .tx { flex: 1; font-weight: 800; font-size: 17px; }
.quest .tx b { font-weight: 900; }
.quest .go { background: var(--ink); color: var(--white); border: 0; border-radius: 12px; padding: 10px 16px; min-height: 44px; cursor: pointer; font-weight: 900; }

/* ---------- cards & buttons ---------- */
.card { background: var(--white); color: var(--ink); border: var(--bw) solid var(--ink); border-radius: var(--r); padding: 16px; box-shadow: 0 6px 0 var(--ink); }
.card.dark { background: var(--ink); color: var(--white); }
.card.navy { background: var(--navy); color: var(--white); }
.card.yellow { background: var(--yellow); } .card.cyan { background: var(--cyan); } .card.pink { background: var(--pink); }
.card.green { background: var(--green); } .card.lav { background: var(--lav); } .card.orange { background: var(--orange); } .card.cream { background: var(--cream); }
.btn { display: inline-flex; align-items: center; justify-content: center; gap: 8px; min-height: 56px; padding: 10px 22px; border-radius: 16px; border: var(--bw) solid var(--ink); background: var(--ink); color: var(--white); font-weight: 900; font-size: 19px; cursor: pointer; box-shadow: 0 4px 0 rgba(17,24,39,.55); user-select: none; -webkit-user-select: none; touch-action: manipulation; }
.btn:active { transform: translateY(3px); box-shadow: 0 1px 0 rgba(17,24,39,.55); }
.btn.light { background: var(--white); color: var(--ink); }
.btn.yellow { background: var(--yellow); color: var(--ink); } .btn.cyan { background: var(--cyan); color: var(--ink); }
.btn.pink { background: var(--pink); color: var(--ink); } .btn.green { background: var(--green); color: var(--ink); }
.btn.orange { background: var(--orange); color: var(--ink); } .btn.lav { background: var(--lav); color: var(--ink); }
.btn.navy { background: var(--navy); color: var(--white); }
.btn.big { min-height: 72px; font-size: 24px; padding: 12px 30px; border-radius: 20px; }
.btn.sm { min-height: 46px; font-size: 16px; padding: 6px 14px; border-radius: 12px; }
.btn[disabled] { opacity: 1; background: var(--white); color: var(--ink); border-style: dashed; cursor: not-allowed; box-shadow: none; }
.chip { display: inline-flex; align-items: center; min-height: 46px; padding: 6px 16px; border-radius: 999px; border: var(--bw) solid var(--ink); background: var(--white); color: var(--ink); font-weight: 900; font-size: 17px; cursor: pointer; user-select: none; touch-action: manipulation; }
.chip.on { background: var(--ink); color: var(--white); }
.chips { display: flex; flex-wrap: wrap; gap: 8px; }
.badge { display: inline-block; padding: 3px 12px; border-radius: 999px; border: 2px solid var(--ink); font-size: 14px; font-weight: 900; background: var(--white); color: var(--ink); }
.badge.easy { background: var(--green); } .badge.medium { background: var(--yellow); } .badge.expert { background: var(--ink); color: var(--white); }
.title { font-size: 34px; margin-bottom: 4px; color: var(--white); text-shadow: 0 3px 0 rgba(0,0,0,.35); }
.sub { color: var(--white); font-size: 18px; margin-bottom: 12px; }
.row { display: flex; gap: 10px; flex-wrap: wrap; align-items: center; }
.grid { display: grid; gap: 14px; }
input[type=text], input[type=search], input[type=password], textarea, select { background: var(--white); color: var(--ink); border: var(--bw) solid var(--ink); border-radius: 14px; padding: 10px 14px; min-height: 54px; font-weight: 800; width: 100%; }
input::placeholder, textarea::placeholder { color: #374151; font-weight: 700; }

/* ---------- home tiles ---------- */
.tiles { grid-template-columns: repeat(auto-fit, minmax(250px, 1fr)); }
.tile { display: flex; flex-direction: column; gap: 6px; text-align: left; border: var(--bw) solid var(--ink); border-radius: 24px; padding: 18px; min-height: 170px; cursor: pointer; box-shadow: 0 8px 0 var(--ink); position: relative; color: var(--ink); }
.tile:active { transform: translateY(4px); box-shadow: 0 4px 0 var(--ink); }
.tile .big { font-size: 56px; line-height: 1; }
.tile h3 { font-size: 26px; }
.tile p { font-size: 17px; font-weight: 800; margin: 0; }
.tile.dark { color: var(--white); }
.tile .tag { position: absolute; top: 12px; right: 12px; background: var(--ink); color: var(--white); border-radius: 999px; padding: 3px 12px; font-size: 13px; font-weight: 900; border: 2px solid var(--white); }
.tile.soon { cursor: default; }

/* ---------- arena ---------- */
.filters { display: grid; gap: 10px; margin-bottom: 14px; }
.cgrid { grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 12px; }
.ccard { background: var(--white); color: var(--ink); border: var(--bw) solid var(--ink); border-radius: 20px; padding: 10px 8px 12px; text-align: center; cursor: pointer; box-shadow: 0 5px 0 var(--ink); min-height: 170px; display: flex; flex-direction: column; align-items: center; gap: 4px; touch-action: manipulation; }
.ccard:active { transform: translateY(3px); box-shadow: 0 2px 0 var(--ink); }
.ccard .fl { font-size: 64px; line-height: 1.1; display: inline-block; }
.ccard .nm { font-weight: 900; font-size: 17px; line-height: 1.15; }
.ccard .cp { font-weight: 800; font-size: 14px; }
@media (hover: hover) { .ccard:hover { background: var(--navy); color: var(--white); } }
.wave { animation: wave 0.9s ease-in-out; }
@keyframes wave { 0% { transform: rotate(0) scale(1); } 15% { transform: rotate(-14deg) scale(1.35); } 35% { transform: rotate(12deg) scale(1.5); } 55% { transform: rotate(-9deg) scale(1.4); } 75% { transform: rotate(6deg) scale(1.2); } 100% { transform: rotate(0) scale(1); } }
.bounce { animation: bounce .5s ease; } @keyframes bounce { 0%,100% { transform: translateY(0); } 40% { transform: translateY(-14px) scale(1.08); } }
.shake { animation: shake .45s ease; } @keyframes shake { 0%,100% { transform: translateX(0); } 20% { transform: translateX(-10px); } 40% { transform: translateX(9px); } 60% { transform: translateX(-6px); } 80% { transform: translateX(4px); } }
.pulse { animation: pulse .35s ease-out; } @keyframes pulse { 0% { transform: scale(1); } 40% { transform: scale(1.18); } 100% { transform: scale(1); } }

/* ---------- modal / sheet ---------- */
.overlay { position: fixed; inset: 0; background: rgba(17,24,39,.78); z-index: 80; display: flex; align-items: flex-end; justify-content: center; padding: 10px; }
.sheet { background: var(--white); color: var(--ink); border: var(--bw) solid var(--ink); border-radius: 28px; width: 100%; max-width: 760px; max-height: 92vh; overflow-y: auto; padding: 18px; box-shadow: 0 8px 0 var(--ink); }
.sheet .bigflag { font-size: 130px; line-height: 1.1; text-align: center; display: block; margin: 0; cursor: pointer; }
.sheet .name { font-size: 34px; text-align: center; }
.facts { display: grid; grid-template-columns: repeat(auto-fit, minmax(210px, 1fr)); gap: 10px; margin: 12px 0; }
.fact { background: var(--cream); border: 3px solid var(--ink); border-radius: 16px; padding: 10px 14px; color: var(--ink); }
.fact small { display: block; font-size: 13px; font-weight: 900; text-transform: uppercase; letter-spacing: .5px; }
.fact b { font-size: 21px; font-weight: 900; }
.closex { float: right; }

/* ---------- quiz ---------- */
.qbox { text-align: center; }
.qflag { font-size: 120px; line-height: 1.1; }
.qtext { font-size: 28px; font-weight: 900; margin: 6px 0 14px; }
.opts { grid-template-columns: repeat(2, 1fr); gap: 12px; }
@media (max-width: 560px) { .opts { grid-template-columns: 1fr; } }
.opt { min-height: 82px; border: var(--bw) solid var(--ink); border-radius: 18px; background: var(--white); color: var(--ink); font-weight: 900; font-size: 22px; cursor: pointer; padding: 10px 14px; box-shadow: 0 5px 0 var(--ink); touch-action: manipulation; }
.opt:active { transform: translateY(3px); }
@media (hover: hover) { .opt:hover:not([disabled]) { background: var(--navy); color: var(--white); } }
.opt.right { background: var(--good); color: var(--white); } .opt.wrong { background: var(--bad); color: var(--white); }
.opt[disabled] { cursor: default; }
.stats { display: flex; gap: 10px; flex-wrap: wrap; justify-content: center; margin: 10px 0; }
.stat { background: var(--white); color: var(--ink); border: 3px solid var(--ink); border-radius: 14px; padding: 6px 14px; font-weight: 900; min-width: 96px; text-align: center; }
.stat small { display: block; font-size: 12px; text-transform: uppercase; }
.stat b { font-size: 24px; }
.verdict { min-height: 40px; font-size: 22px; font-weight: 900; margin: 10px 0; }
.verdict.ok { color: var(--ink); background: var(--green); border: 3px solid var(--ink); border-radius: 14px; padding: 8px 14px; }
.verdict.no { color: var(--white); background: var(--bad); border: 3px solid var(--ink); border-radius: 14px; padding: 8px 14px; }

/* ---------- python lab ---------- */
.lab { display: grid; grid-template-columns: minmax(0, 1.15fr) minmax(0, 1fr); gap: 14px; align-items: start; }
.lab > div, .designer > div { min-width: 0; }
@media (max-width: 900px) { .lab { grid-template-columns: minmax(0, 1fr); } }
.levels { display: flex; gap: 8px; overflow-x: auto; padding: 4px 2px 10px; }
.lvl { flex: none; min-width: 74px; min-height: 68px; border: var(--bw) solid var(--ink); border-radius: 18px; background: var(--white); color: var(--ink); display: flex; flex-direction: column; align-items: center; justify-content: center; font-weight: 900; font-size: 14px; cursor: pointer; box-shadow: 0 4px 0 var(--ink); }
.lvl .e { font-size: 26px; line-height: 1; }
.lvl.on { background: var(--ink); color: var(--white); }
.lvl.locked { background: #E5E7EB; color: var(--ink); border-style: dashed; box-shadow: none; }
.mchips { display: flex; gap: 8px; flex-wrap: wrap; margin: 8px 0; }
.mchip { min-height: 50px; padding: 6px 14px; border: var(--bw) solid var(--ink); border-radius: 14px; background: var(--white); color: var(--ink); font-weight: 900; cursor: pointer; }
.mchip.on { background: var(--ink); color: var(--white); }
.mchip.done { background: var(--green); color: var(--ink); }
.mchip.on.done { background: var(--ink); color: var(--white); }
.story { font-size: 19px; line-height: 1.35; font-weight: 800; }
.editor-wrap { background: var(--ink); border: var(--bw) solid var(--ink); border-radius: 18px; overflow: hidden; }
.editor-head { color: var(--white); background: var(--ink); padding: 8px 12px; font-weight: 900; font-size: 16px; display: flex; justify-content: space-between; align-items: center; }
textarea.code { display: block; width: 100%; min-height: 220px; resize: vertical; background: var(--white); color: var(--ink); font-family: var(--mono); font-size: 21px; font-weight: 700; line-height: 1.5; border: 0; border-radius: 0; padding: 12px 14px; tab-size: 4; white-space: pre; overflow: auto; }
.symbar { display: flex; gap: 6px; flex-wrap: wrap; padding: 8px; background: var(--ink); }
.symbar button { min-width: 48px; min-height: 48px; border: 2px solid var(--white); background: var(--white); color: var(--ink); border-radius: 12px; font-family: var(--mono); font-size: 22px; font-weight: 900; cursor: pointer; }
.symbar button.wide { padding: 0 12px; font-family: var(--font); font-size: 15px; }
.console { background: var(--ink); color: var(--white); border: var(--bw) solid var(--ink); border-radius: 16px; padding: 10px 14px; font-family: var(--mono); font-size: 19px; font-weight: 700; min-height: 90px; max-height: 260px; overflow: auto; white-space: pre-wrap; word-break: break-word; }
.console .err { background: var(--bad); color: var(--white); border-radius: 10px; padding: 8px 10px; font-family: var(--font); font-size: 18px; margin-top: 6px; }
.console .lbl { font-family: var(--font); font-size: 13px; text-transform: uppercase; font-weight: 900; }
.stage { border: var(--bw) solid var(--ink); border-radius: 22px; padding: 16px; text-align: center; background: var(--navy); color: var(--white); position: relative; overflow: hidden; min-height: 330px; }
.stage .sflag { font-size: 120px; line-height: 1.1; display: inline-block; transition: transform .15s; }
.stage .sinfo { display: grid; gap: 8px; margin-top: 10px; text-align: left; }
.stage .sinfo div { background: var(--white); color: var(--ink); border: 3px solid var(--ink); border-radius: 14px; padding: 8px 12px; font-weight: 900; font-size: 18px; }
.stage .sinfo small { font-size: 12px; text-transform: uppercase; display: block; }
.stage .hint { font-size: 20px; padding: 40px 10px; }
.stage .wiggle { animation: wave .9s ease-in-out; }
.beatbars { display: flex; gap: 6px; justify-content: center; align-items: flex-end; height: 48px; margin: 8px 0; }
.beatbars i { width: 16px; background: var(--yellow); border: 2px solid var(--ink); border-radius: 6px; height: 12px; transition: height .08s; }
.overlay.center { align-items: center; }
.loading { text-align: center; padding: 18px; font-size: 22px; }
.spinner { font-size: 44px; display: inline-block; animation: spin 1.2s linear infinite; } @keyframes spin { to { transform: rotate(360deg); } }
.pass { background: var(--green); color: var(--ink); border: 3px solid var(--ink); border-radius: 16px; padding: 12px 14px; font-weight: 900; margin-top: 10px; }
.notyet { background: var(--yellow); color: var(--ink); border: 3px solid var(--ink); border-radius: 16px; padding: 12px 14px; font-weight: 900; margin-top: 10px; }

/* ---------- language lab ---------- */
.lgrid { grid-template-columns: repeat(auto-fill, minmax(110px, 1fr)); gap: 10px; }
.lcard { background: var(--white); color: var(--ink); border: var(--bw) solid var(--ink); border-radius: 18px; min-height: 104px; display: flex; flex-direction: column; align-items: center; justify-content: center; font-weight: 900; cursor: pointer; box-shadow: 0 4px 0 var(--ink); padding: 6px; text-align: center; }
.lcard .ch { font-size: 40px; line-height: 1.1; }
.lcard .sd { font-size: 14px; }
.lcard.got { background: var(--green); }
.bigchar { font-size: 140px; line-height: 1.1; text-align: center; }
canvas.trace { width: 100%; max-width: 420px; aspect-ratio: 1; background: var(--white); border: var(--bw) solid var(--ink); border-radius: 18px; touch-action: none; display: block; margin: 8px auto; }
table.tbl { width: 100%; border-collapse: collapse; background: var(--white); color: var(--ink); }
table.tbl th { background: var(--ink); color: var(--white); text-align: left; padding: 10px; font-size: 15px; position: sticky; top: 0; }
table.tbl td { border-bottom: 2px solid var(--ink); padding: 8px 10px; font-weight: 800; font-size: 17px; }
.tblwrap { max-height: 60vh; overflow: auto; border: var(--bw) solid var(--ink); border-radius: 16px; }
.tr-out { background: var(--cream); color: var(--ink); border: var(--bw) solid var(--ink); border-radius: 16px; padding: 14px; font-size: 28px; font-weight: 900; min-height: 80px; }

/* ---------- flag designer ---------- */
.designer { display: grid; grid-template-columns: minmax(0, 1.1fr) minmax(0, 1fr); gap: 14px; align-items: start; }
@media (max-width: 900px) { .designer { grid-template-columns: minmax(0, 1fr); } }
.flagcanvas { background: var(--white); border: var(--bw) solid var(--ink); border-radius: 18px; touch-action: none; width: 100%; display: block; }
.swatches { display: flex; flex-wrap: wrap; gap: 8px; }
.sw { width: 52px; height: 52px; border-radius: 14px; border: var(--bw) solid var(--ink); cursor: pointer; }
.sw.on { outline: 4px solid var(--ink); outline-offset: 2px; }
.stickers { display: grid; grid-template-columns: repeat(auto-fill, minmax(58px, 1fr)); gap: 8px; max-height: 230px; overflow: auto; }
.stk { min-height: 58px; font-size: 32px; background: var(--white); color: var(--ink); border: 3px solid var(--ink); border-radius: 14px; cursor: pointer; }
.pat { width: 78px; height: 56px; border: 3px solid var(--ink); border-radius: 10px; background: var(--white); cursor: pointer; padding: 0; overflow: hidden; }
.pat.on { outline: 4px solid var(--ink); outline-offset: 2px; }
.mine { grid-template-columns: repeat(auto-fill, minmax(190px, 1fr)); gap: 12px; }

/* ---------- profile screen ---------- */
.keypad { display: grid; grid-template-columns: repeat(3, 84px); gap: 10px; justify-content: center; margin: 12px 0; }
.keypad button { min-height: 72px; font-size: 28px; border: var(--bw) solid var(--ink); border-radius: 18px; background: var(--white); color: var(--ink); font-weight: 900; cursor: pointer; }
.dots { display: flex; gap: 12px; justify-content: center; margin: 8px 0; }
.dots i { width: 24px; height: 24px; border: 3px solid var(--ink); border-radius: 50%; background: var(--white); }
.dots i.on { background: var(--ink); }
.avatars { display: grid; grid-template-columns: repeat(auto-fill, minmax(72px, 1fr)); gap: 8px; }
.avatars button { font-size: 40px; min-height: 72px; border: 3px solid var(--ink); border-radius: 18px; background: var(--white); cursor: pointer; }
.avatars button.on { background: var(--yellow); outline: 4px solid var(--ink); outline-offset: 2px; }

/* ---------- toast & confetti ---------- */
#toast { position: fixed; left: 50%; top: 80px; transform: translateX(-50%); z-index: 120; background: var(--ink); color: var(--white); border: 3px solid var(--yellow); border-radius: 18px; padding: 12px 20px; font-weight: 900; font-size: 19px; box-shadow: 0 6px 0 rgba(0,0,0,.4); max-width: 92vw; text-align: center; }
.confetti { position: fixed; z-index: 110; pointer-events: none; font-size: 34px; animation: fall 1.6s ease-in forwards; }
@keyframes fall { 0% { transform: translateY(-20px) rotate(0); opacity: 1; } 100% { transform: translateY(100vh) rotate(540deg); opacity: .9; } }
.splash { text-align: center; padding: 30px 10px; }
.splash .logo { font-size: 90px; }
.hl { color: var(--yellow); }
.note { font-size: 15px; font-weight: 800; }
.card .note, .card p { color: var(--ink); }
.card.dark p, .card.navy p, .card.dark .note, .card.navy .note { color: var(--white); }
