:root {
  --bg1: #4b2bb8;
  --bg2: #2477e6;
  --card: #ffffff;
  --ink: #1f1a3a;
  --muted: #6b6788;
  --line: #e6e3f3;
  --soft: #f4f2fc;
  --brand: #ff5e7e;
  --brand-dark: #e2405f;
  --accent: #ffc83d;
  --good: #22a55a;
  --good-soft: #e3f7ea;
  --warn: #f08a00;
  --warn-soft: #fff1dc;
  --radius: 18px;
  --shadow: 0 10px 30px rgba(20, 10, 70, .25);
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  font-family: 'Nunito', system-ui, sans-serif;
  color: var(--ink);
  background: var(--bg1);
  background-image:
    radial-gradient(circle at 15% 20%, rgba(255,255,255,.10) 0 2px, transparent 3px),
    radial-gradient(circle at 70% 60%, rgba(255,255,255,.08) 0 2px, transparent 3px),
    linear-gradient(135deg, var(--bg1), var(--bg2));
  background-size: 48px 48px, 64px 64px, 100% 100%;
  background-attachment: fixed;
  -webkit-font-smoothing: antialiased;
}
h1, h2, h3, .display { font-family: 'Baloo 2', 'Nunito', sans-serif; margin: 0; }
button, input, select, textarea { font: inherit; color: inherit; }
.hidden { display: none !important; }

/* ---------- shared bits ---------- */
.card { background: var(--card); border-radius: var(--radius); box-shadow: var(--shadow); }
.btn {
  border: 0; border-radius: 14px; padding: 12px 20px; font-weight: 800; cursor: pointer;
  background: var(--brand); color: #fff; box-shadow: 0 4px 0 var(--brand-dark);
  transition: transform .08s, box-shadow .08s, filter .15s; font-size: 16px;
}
.btn:hover { filter: brightness(1.05); }
.btn:active { transform: translateY(3px); box-shadow: 0 1px 0 var(--brand-dark); }
.btn:disabled { opacity: .5; cursor: not-allowed; transform: none; }
.btn.secondary { background: var(--soft); color: var(--ink); box-shadow: 0 4px 0 #d8d3ee; }
.btn.yellow { background: var(--accent); color: #3a2a00; box-shadow: 0 4px 0 #d9a414; }
.btn.small { padding: 7px 12px; font-size: 14px; border-radius: 10px; }
.btn.big { padding: 15px 26px; font-size: 19px; }
.input {
  width: 100%; border: 2px solid var(--line); border-radius: 12px; padding: 11px 14px; background: #fff;
  outline: none; transition: border-color .15s;
}
.input:focus { border-color: #8b74f0; }
label.lbl { display: block; font-weight: 800; font-size: 13px; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); margin: 0 0 6px; }

.logo { font-family: 'Baloo 2', sans-serif; font-weight: 800; color: #fff; letter-spacing: -.5px; line-height: 1; text-shadow: 0 4px 0 rgba(0,0,0,.18); }
.logo span { color: var(--accent); }

/* ---------- home ---------- */
#home { min-height: 100%; display: flex; flex-direction: column; align-items: center; justify-content: center; padding: 32px 16px; gap: 22px; }
#home .logo { font-size: clamp(52px, 11vw, 92px); text-align: center; }
.tagline { color: #e8e4ff; font-weight: 700; font-size: 18px; text-align: center; margin-top: -8px; }
.home-card { width: 100%; max-width: 420px; padding: 26px; display: flex; flex-direction: column; gap: 14px; }
.or { display: flex; align-items: center; gap: 10px; color: var(--muted); font-weight: 700; font-size: 13px; }
.or::before, .or::after { content: ''; flex: 1; height: 2px; background: var(--line); }
.join-row { display: flex; gap: 8px; }
.join-row .input { text-transform: uppercase; letter-spacing: .15em; font-weight: 800; }
.demo-link { color: #fff; opacity: .85; font-weight: 700; text-decoration: underline; background: none; border: 0; cursor: pointer; }
.how { max-width: 620px; color: #e8e4ff; font-size: 15px; text-align: center; line-height: 1.5; }

/* ---------- lobby ---------- */
#lobby { min-height: 100%; padding: 24px 16px 40px; display: flex; flex-direction: column; align-items: center; gap: 18px; }
#lobby .logo { font-size: 46px; }
.lobby-grid { width: 100%; max-width: 980px; display: grid; grid-template-columns: 300px 1fr; gap: 18px; }
.lobby-side { padding: 20px; display: flex; flex-direction: column; gap: 14px; }
.room-code { font-family: 'Baloo 2', sans-serif; font-size: 44px; font-weight: 800; letter-spacing: .12em; text-align: center; background: var(--soft); border-radius: 14px; padding: 6px 0 0; color: #5a3fd0; }
.lobby-players { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 6px; }
.lobby-players li { display: flex; align-items: center; gap: 10px; padding: 7px 10px; border-radius: 12px; background: var(--soft); font-weight: 800; }
.settings { padding: 22px; display: flex; flex-direction: column; gap: 20px; }
.settings h2 { font-size: 26px; }
.seg { display: flex; flex-wrap: wrap; gap: 8px; }
.seg button {
  border: 2px solid var(--line); background: #fff; border-radius: 12px; padding: 9px 14px; font-weight: 800; cursor: pointer;
}
.seg button.on { border-color: #6b4ff0; background: #efeaff; color: #3d25b5; }
.seg button:disabled { cursor: default; }
.hint { color: var(--muted); font-size: 14px; margin-top: 6px; }
.range-row { display: flex; align-items: center; gap: 12px; }
.range-row input[type=range] { flex: 1; accent-color: #6b4ff0; }
.range-row .num { width: 84px; text-align: center; font-weight: 800; }
textarea.input { min-height: 130px; resize: vertical; }
.count-ok { color: var(--good); font-weight: 800; }
.count-bad { color: var(--brand-dark); font-weight: 800; }
.two { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; }
.check { display: flex; gap: 10px; align-items: flex-start; margin-top: 10px; font-weight: 700; font-size: 14px; cursor: pointer; }
.check input { width: 18px; height: 18px; margin-top: 1px; accent-color: #6b4ff0; flex-shrink: 0; }
.warn-box { background: var(--warn-soft); color: #8a4b00; padding: 10px 14px; border-radius: 12px; font-weight: 700; }
.warn-box a { color: inherit; }
.kick { border: 0; background: transparent; color: var(--muted); cursor: pointer; font-size: 15px; padding: 2px 6px; border-radius: 8px; margin-left: auto; }
.kick:hover { background: #ffe3e8; color: var(--brand-dark); }
.waiting { text-align: center; color: var(--muted); font-weight: 700; }

/* ---------- game ---------- */
#game { height: 100%; display: flex; flex-direction: column; padding: 10px 12px 12px; gap: 10px; }
.topbar { display: grid; grid-template-columns: auto 1fr auto; align-items: center; gap: 14px; }
.topbar .logo { font-size: 30px; }
.word-box { padding: 8px 16px; display: flex; align-items: center; justify-content: center; gap: 14px; min-height: 58px; }
.round-info { font-weight: 800; color: var(--muted); font-size: 13px; text-transform: uppercase; white-space: nowrap; }
.level { display: inline-block; padding: 2px 8px; border-radius: 999px; font-size: 12px; font-weight: 800; color: #fff; margin-left: 6px; vertical-align: 1px; }
.level.easy { background: #22a55a; } .level.medium { background: #2477e6; } .level.hard { background: #f08a00; } .level.extreme { background: #d6264b; }
.mask-wrap { flex: 0 1 auto; min-width: 0; }
.mask { display: flex; flex-wrap: wrap; justify-content: center; gap: 5px; }
.mask .ch { width: 24px; height: 32px; border-bottom: 3px solid var(--ink); font-family: 'Baloo 2', sans-serif; font-size: 24px; font-weight: 800; display: flex; align-items: flex-end; justify-content: center; line-height: 1; padding-bottom: 2px; text-transform: uppercase; }
.mask .sp { width: 14px; border: 0; }
.mask .pun { border: 0; width: 12px; }
.mask.solved .ch { color: var(--good); border-color: var(--good); }
.letters { font-size: 12px; color: var(--muted); font-weight: 800; }
.timer { width: 58px; height: 58px; border-radius: 50%; background: #fff; box-shadow: var(--shadow); display: grid; place-items: center; font-family: 'Baloo 2', sans-serif; font-size: 22px; font-weight: 800; position: relative; }
.timer svg { position: absolute; inset: 0; transform: rotate(-90deg); }
.timer.low { color: var(--brand-dark); animation: pulse 1s infinite; }
@keyframes pulse { 50% { transform: scale(1.08); } }

.game-main { flex: 1; min-height: 0; display: grid; grid-template-columns: 220px 1fr 300px; gap: 10px; }
.players { padding: 10px; overflow-y: auto; display: flex; flex-direction: column; gap: 6px; }
.player { display: grid; grid-template-columns: 34px 1fr auto; align-items: center; gap: 8px; padding: 7px 8px; border-radius: 12px; background: var(--soft); }
.player.me { outline: 2px solid #b8a9f7; }
.player.got { background: var(--good-soft); }
.avatar { width: 34px; height: 34px; border-radius: 50%; display: grid; place-items: center; color: #fff; font-weight: 800; font-family: 'Baloo 2', sans-serif; font-size: 17px; }
.p-name { font-weight: 800; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: 15px; }
.p-sub { font-size: 12px; color: var(--muted); font-weight: 700; }
.p-score { font-family: 'Baloo 2', sans-serif; font-weight: 800; font-size: 18px; }
.rank { font-size: 11px; color: var(--muted); font-weight: 800; }

.board { display: flex; flex-direction: column; align-items: center; gap: 8px; min-width: 0; min-height: 0; }
.canvas-wrap {
  position: relative; width: min(100%, calc((100vh - 190px) * 4 / 3)); aspect-ratio: 4 / 3;
  background: #fff; border-radius: 14px; box-shadow: var(--shadow); overflow: hidden; flex-shrink: 0;
}
.canvas-wrap canvas { position: absolute; inset: 0; width: 100%; height: 100%; touch-action: none; }
.cursor { position: absolute; left: 0; top: 0; width: 34px; height: 34px; pointer-events: none; transition: opacity .2s; z-index: 3; filter: drop-shadow(0 2px 2px rgba(0,0,0,.35)); }
.cursor svg { width: 100%; height: 100%; }
.ai-badge { position: absolute; left: 10px; top: 10px; background: rgba(31,26,58,.82); color: #fff; padding: 4px 10px; border-radius: 999px; font-weight: 800; font-size: 13px; z-index: 4; display: flex; align-items: center; gap: 6px; }
.ai-badge .dot { width: 8px; height: 8px; border-radius: 50%; background: #3ee07a; animation: blink 1s infinite; }
@keyframes blink { 50% { opacity: .3; } }

.overlay { position: absolute; inset: 0; z-index: 5; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 12px; text-align: center; padding: 20px; background: rgba(255,255,255,.88); backdrop-filter: blur(2px); }
.overlay h2 { font-size: clamp(26px, 4vw, 40px); line-height: 1.1; }
.overlay p { margin: 0; color: var(--muted); font-weight: 700; }
.overlay.dark { background: rgba(31, 26, 58, .78); color: #fff; }
.overlay.dark p { color: #ddd8ff; }
.reveal-word { font-family: 'Baloo 2', sans-serif; font-size: clamp(36px, 7vw, 64px); font-weight: 800; color: var(--accent); text-transform: uppercase; line-height: 1; }
.countdown { font-family: 'Baloo 2', sans-serif; font-size: 120px; font-weight: 800; color: #5a3fd0; line-height: 1; }
.pencil-anim { font-size: 54px; animation: wiggle 1s ease-in-out infinite; }
@keyframes wiggle { 0%,100% { transform: rotate(-12deg); } 50% { transform: rotate(12deg) translateY(-6px); } }
.results { list-style: none; padding: 0; margin: 4px 0; width: min(360px, 100%); display: flex; flex-direction: column; gap: 6px; }
.results li { display: flex; justify-content: space-between; background: rgba(255,255,255,.12); padding: 8px 14px; border-radius: 12px; font-weight: 800; font-size: 18px; }
.results li:first-child { background: var(--accent); color: #3a2a00; font-size: 22px; }

.toolbar { display: flex; flex-wrap: wrap; align-items: center; justify-content: center; gap: 10px; padding: 8px 12px; width: min(100%, calc((100vh - 190px) * 4 / 3)); }
.tools, .sizes { display: flex; gap: 4px; }
.tool { width: 38px; height: 38px; border-radius: 10px; border: 2px solid transparent; background: var(--soft); display: grid; place-items: center; transition: all .15s; }
.tool svg { width: 22px; height: 22px; }
.tool.on { background: #ffe7ad; border-color: var(--accent); transform: translateY(-2px); }
.size-dot { width: 30px; height: 38px; border-radius: 10px; display: grid; place-items: center; background: var(--soft); border: 2px solid transparent; }
.size-dot i { display: block; border-radius: 50%; background: var(--ink); }
.size-dot.on { border-color: var(--accent); background: #ffe7ad; }
.palette { display: grid; grid-template-columns: repeat(9, 20px); gap: 3px; }
.swatch { width: 20px; height: 20px; border-radius: 5px; border: 1px solid rgba(0,0,0,.15); transition: transform .15s; }
.swatch.on { transform: scale(1.25); box-shadow: 0 0 0 2px #fff, 0 0 0 4px var(--ink); position: relative; z-index: 1; }
.toolbar-note { font-size: 12px; font-weight: 800; color: var(--muted); }

.chat { display: flex; flex-direction: column; min-height: 0; overflow: hidden; }
.chat-head { padding: 10px 14px; font-weight: 800; border-bottom: 2px solid var(--line); display: flex; justify-content: space-between; align-items: center; }
.messages { flex: 1; overflow-y: auto; padding: 8px 10px; display: flex; flex-direction: column; gap: 3px; font-size: 15px; }
.msg { padding: 4px 8px; border-radius: 8px; word-wrap: break-word; line-height: 1.35; }
.msg b { font-weight: 800; }
.msg.m-system { color: var(--muted); font-weight: 700; font-size: 14px; }
.msg.m-correct { background: var(--good-soft); color: var(--good); font-weight: 800; }
.msg.m-reveal { background: #efeaff; color: #3d25b5; font-weight: 800; }
.msg.m-close { background: var(--warn-soft); color: var(--warn); font-weight: 800; }
.msg.m-private { background: var(--good); color: #fff; font-weight: 800; }
.msg.m-guessed { color: var(--good); }
.msg:nth-child(even):not(.m-system):not(.m-correct):not(.m-close):not(.m-private):not(.m-reveal) { background: var(--soft); }
.chat-form { display: flex; gap: 6px; padding: 10px; border-top: 2px solid var(--line); }
.chat-form .input { padding: 10px 12px; }
.icon-btn { border: 0; background: var(--soft); border-radius: 10px; width: 34px; height: 30px; cursor: pointer; font-size: 16px; }

#toast { position: fixed; left: 50%; bottom: 24px; transform: translateX(-50%) translateY(20px); background: #1f1a3a; color: #fff; padding: 12px 18px; border-radius: 12px; font-weight: 700; opacity: 0; pointer-events: none; transition: all .25s; z-index: 50; max-width: calc(100% - 32px); text-align: center; }
#toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }

/* ---------- small screens ---------- */
@media (max-width: 980px) {
  .lobby-grid { grid-template-columns: 1fr; }
  #game { height: auto; min-height: 100%; }
  .game-main { grid-template-columns: 1fr; }
  .board { order: 1; }
  .chat { order: 2; height: 340px; }
  .players { order: 3; flex-direction: row; overflow-x: auto; }
  .player { min-width: 170px; }
  .canvas-wrap, .toolbar { width: 100%; }
  .topbar { grid-template-columns: 1fr auto; }
  .topbar .logo { display: none; }
}
@media (max-width: 560px) {
  .word-box { flex-wrap: wrap; gap: 6px 12px; padding: 10px; }
  .mask-wrap { flex-basis: 100%; order: -1; }
  .two { grid-template-columns: 1fr; }
  .mask .ch { width: 18px; height: 26px; font-size: 19px; }
  .palette { grid-template-columns: repeat(9, 17px); }
  .swatch { width: 17px; height: 17px; }
  .tool { width: 32px; height: 32px; }
  .size-dot { width: 24px; height: 32px; }
  .countdown { font-size: 80px; }
}
