:root {
  --ink: #2c313d;
  --ink-soft: #596276;
  --paper: #ffffff;
  --sky: #cfe8f5;
  --sky-2: #a9d6f0;
  --yellow: #ffc531;
  --yellow-d: #c98f00;
  --blue: #3aa0ff;
  --blue-d: #1a6bc0;
  --green: #2ed37e;
  --green-d: #179456;
  --red: #ff5a5f;
  --red-d: #c0282d;
  --pink: #ff4f8b;
  --radius: 18px;
  --shadow: 0 4px 0 rgba(44, 49, 61, 0.22);
  font-family: 'Fredoka', system-ui, sans-serif;
  color: var(--ink);
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body { background: linear-gradient(#bfe3f7, #e3f3fb 60%, #d7ecd0); min-height: 100%; -webkit-tap-highlight-color: transparent; }
button { font: inherit; color: inherit; cursor: pointer; }
input { font: inherit; }
.hidden { display: none !important; }
.muted { color: var(--ink-soft); }
.small { font-size: 13px; }
a { color: var(--blue-d); }

/* ---------- Buttons ---------- */
.btn {
  border: 3px solid var(--ink); border-radius: 14px; padding: 8px 14px; font-weight: 600;
  background: var(--paper); box-shadow: 0 4px 0 var(--ink); transform: translateY(0);
  transition: transform .08s, box-shadow .08s, filter .15s; white-space: nowrap; display: inline-flex; align-items: center; gap: 6px; justify-content: center;
}
.btn:hover { filter: brightness(1.05); }
.btn:active, .btn.pressed { transform: translateY(3px); box-shadow: 0 1px 0 var(--ink); }
.btn:disabled { opacity: .45; cursor: not-allowed; transform: none; box-shadow: 0 4px 0 var(--ink); }
.btn.big { font-size: 19px; padding: 14px 18px; border-radius: 18px; }
.btn.small { font-size: 14px; padding: 5px 10px; border-radius: 12px; box-shadow: 0 3px 0 var(--ink); }
.btn.icon { padding: 5px 8px; }
.btn.yellow { background: var(--yellow); }
.btn.blue { background: var(--blue); color: #fff; }
.btn.green { background: var(--green); color: #fff; }
.btn.white { background: #fff; }
.btn.red { background: var(--red); color: #fff; }
.btn.play { background: linear-gradient(var(--pink), #e23a77); color: #fff; font-size: 18px; padding: 10px 22px; }
.btn.play.stop { background: linear-gradient(#596276, #2c313d); }
.link { background: none; border: 0; color: var(--blue-d); font-weight: 600; padding: 0; }

.panel {
  background: var(--paper); border: 3px solid var(--ink); border-radius: var(--radius);
  box-shadow: var(--shadow); padding: 14px;
}
.panel h3 { margin: 0 0 10px; font-size: 15px; text-transform: uppercase; letter-spacing: .06em; color: var(--ink-soft); }

/* ---------- Home ---------- */
.screen { min-height: 100vh; }
#home { position: relative; overflow: hidden; display: flex; flex-direction: column; }
.home-bg { position: absolute; inset: 0; background: url(sprites/k/colored_grass.png) repeat-x bottom / auto 100%; opacity: .9; }
.home-inner { position: relative; flex: 1; display: grid; grid-template-columns: 1.1fr 1fr; gap: 32px; align-items: center; max-width: 1100px; margin: 0 auto; padding: 40px 24px; width: 100%; }
.logo-wrap { position: relative; text-align: center; }
.logo { display: inline-flex; gap: 6px; }
.logo span {
  font-size: clamp(90px, 16vw, 170px); font-weight: 700; line-height: .9; color: #fff;
  -webkit-text-stroke: 7px var(--ink); paint-order: stroke fill; text-shadow: 0 10px 0 var(--ink);
  display: inline-block; animation: bob 2.4s ease-in-out infinite;
}
.logo span:nth-child(1) { color: var(--yellow); animation-delay: 0s; }
.logo span:nth-child(2) { color: var(--pink); animation-delay: .2s; }
.logo span:nth-child(3) { color: var(--blue); animation-delay: .4s; }
@keyframes bob { 50% { transform: translateY(-10px) rotate(-2deg); } }
.tagline { font-size: clamp(18px, 2.4vw, 26px); font-weight: 500; margin: 14px 0 0; }
.tagline b { color: var(--pink); }
.floaters .fl { position: absolute; width: 70px; animation: float 5s ease-in-out infinite; filter: drop-shadow(0 6px 0 rgba(0,0,0,.12)); }
.f1 { top: -40px; left: 4%; width: 110px !important; }
.f2 { top: 10px; right: 2%; animation-delay: -1s !important; }
.f3 { bottom: -70px; left: 16%; width: 54px !important; animation-delay: -2s !important; }
.f4 { bottom: -90px; right: 18%; width: 52px !important; animation-delay: -3s !important; }
.f5 { top: 46%; left: -6%; width: 90px !important; animation-delay: -1.5s !important; }
@keyframes float { 50% { transform: translateY(-14px) rotate(4deg); } }
.home-card { display: flex; flex-direction: column; gap: 12px; padding: 22px; }
.field { display: flex; flex-direction: column; gap: 6px; font-weight: 600; }
.field input { border: 3px solid var(--ink); border-radius: 14px; padding: 12px 14px; font-size: 18px; outline: none; }
.field input:focus { border-color: var(--blue); }
.modes { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }
.mode {
  border: 3px solid var(--ink); border-radius: 14px; background: #f3f7fb; padding: 10px; text-align: left; display: flex; flex-direction: column; gap: 4px;
  box-shadow: 0 3px 0 var(--ink);
}
.mode b { font-size: 15px; }
.mode small { color: var(--ink-soft); font-size: 12px; line-height: 1.25; }
.mode .e { font-size: 22px; }
.mode.on { background: #fff6d6; border-color: var(--ink); outline: 3px solid var(--yellow); }
.home-actions { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; margin-top: 4px; }
.credits { position: relative; text-align: center; font-size: 12px; padding: 10px; color: #2c313d; }

/* ---------- Game ---------- */
#game { display: flex; flex-direction: column; height: 100vh; height: 100dvh; }
.topbar { display: flex; align-items: center; gap: 10px; padding: 10px 14px; background: #fff; border-bottom: 3px solid var(--ink); flex-wrap: wrap; }
.logo-sm { font-weight: 700; font-size: 22px; background: var(--yellow); border: 3px solid var(--ink); border-radius: 12px; padding: 0 10px; box-shadow: 0 3px 0 var(--ink); }
.lvl-btn { display: inline-flex; align-items: center; gap: 8px; font-weight: 700; font-size: 18px; background: none; border: 0; padding: 4px 6px; border-radius: 10px; }
.lvl-btn:hover { background: #eef5fb; }
.caret { color: var(--ink-soft); font-size: 14px; }
.badge { background: #eef5fb; border: 2px solid var(--ink); border-radius: 999px; padding: 3px 10px; font-size: 14px; font-weight: 600; }
.spacer { flex: 1; }
.conn { font-size: 13px; color: var(--ink-soft); }
.conn.off { color: var(--red-d); }
#btnVoice.on { background: var(--green); color: #fff; }

.layout { flex: 1; display: grid; grid-template-columns: 1fr 330px; gap: 14px; padding: 14px; min-height: 0; }
.stage-col { display: flex; flex-direction: column; gap: 10px; min-width: 0; min-height: 0; }
.stage { position: relative; border: 3px solid var(--ink); border-radius: var(--radius); overflow: hidden; box-shadow: var(--shadow); background: var(--sky); aspect-ratio: 12 / 7; max-height: calc(100dvh - 190px); margin: 0 auto; width: 100%; }
#cv { display: block; width: 100%; height: 100%; touch-action: none; }
.turn-banner {
  position: absolute; top: 10px; left: 50%; transform: translateX(-50%); background: #fff; border: 3px solid var(--ink); border-radius: 999px;
  padding: 4px 16px; font-weight: 700; box-shadow: 0 3px 0 var(--ink); white-space: nowrap; pointer-events: none; font-size: 15px;
}
.turn-banner.mine { background: var(--yellow); animation: pulse 1.4s ease-in-out infinite; }
@keyframes pulse { 50% { transform: translateX(-50%) scale(1.06); } }
.role-banner { position: absolute; bottom: 10px; left: 10px; max-width: calc(100% - 20px); background: var(--ink); color: #fff; border-radius: 12px; padding: 6px 12px; font-weight: 600; font-size: 14px; }
.role-banner.sab { background: #b3132d; }
.run-banner { position: absolute; bottom: 52px; left: 50%; transform: translateX(-50%); background: #fff; border: 3px solid var(--ink); border-radius: 14px; padding: 6px 16px; font-weight: 700; box-shadow: 0 3px 0 var(--ink); }
.run-banner.ok { background: var(--green); color: #fff; }
.run-banner.bad { background: #ffe1e2; }

.actionbar { display: flex; justify-content: space-between; align-items: center; gap: 10px; flex-wrap: wrap; }
.edit-tools, .play-tools { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.edit-tools.off .tool, .edit-tools.off #btnConfirm { opacity: .35; pointer-events: none; }
.tool { width: 44px; height: 44px; border: 3px solid var(--ink); border-radius: 12px; background: #fff; font-size: 20px; box-shadow: 0 3px 0 var(--ink); }
.tool:active { transform: translateY(2px); box-shadow: 0 1px 0 var(--ink); }
.tool.danger { background: #ffe1e2; }
.help { margin: 0; text-align: center; }

.side { display: flex; flex-direction: column; gap: 12px; overflow-y: auto; min-height: 0; padding-bottom: 6px; }
.goal-head { display: flex; gap: 10px; align-items: flex-start; }
.goal-emoji { font-size: 32px; line-height: 1; }
.goal-head h2 { margin: 0; font-size: 20px; }
.goal-head p { margin: 2px 0 0; color: var(--ink-soft); font-size: 14px; line-height: 1.3; }
.goal-meta { display: flex; gap: 8px; align-items: center; margin-top: 10px; flex-wrap: wrap; }
.chip { background: #eef5fb; border-radius: 999px; padding: 3px 10px; font-size: 13px; font-weight: 600; }
.chip.warn { background: #ffe1e2; color: var(--red-d); }
.hint { background: #fff6d6; border-radius: 12px; padding: 8px 10px; margin: 10px 0 0; font-size: 14px; }

.players { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 6px; }
.players li { display: flex; align-items: center; gap: 8px; padding: 6px 8px; border-radius: 12px; font-weight: 500; }
.players li.turn { background: #fff6d6; outline: 2px solid var(--yellow); }
.players li.off { opacity: .45; }
.dot { width: 18px; height: 18px; border-radius: 50%; border: 3px solid var(--ink); flex: none; transition: box-shadow .1s; }
.players .tags { margin-left: auto; display: flex; gap: 4px; font-size: 14px; }
.players .kit { display: flex; gap: 2px; }
.players .kit img { width: 18px; height: 18px; object-fit: contain; }

.toolbox { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }
.tb-item {
  position: relative; border: 3px solid var(--ink); border-radius: 14px; background: #f3f7fb; padding: 8px 4px 6px;
  display: flex; flex-direction: column; align-items: center; gap: 4px; box-shadow: 0 3px 0 var(--ink); min-height: 86px;
}
.tb-item:hover:not(:disabled) { background: #fff6d6; }
.tb-item:disabled { opacity: .4; cursor: not-allowed; }
.tb-item.sel { background: var(--yellow); }
.tb-item .ic { height: 40px; width: 56px; display: grid; place-items: center; }
.tb-item .ic img { max-width: 56px; max-height: 40px; }
.tb-item .ic .portal-ic { width: 16px; height: 38px; border-radius: 50%; border: 5px solid; }
.tb-item .ic .conv-ic { width: 52px; height: 16px; border-radius: 8px; background: repeating-linear-gradient(90deg, #4b5263 0 8px, #ffd23f 8px 11px); border: 3px solid var(--ink); }
.tb-item .nm { font-size: 12px; font-weight: 600; text-align: center; line-height: 1.1; }
.tb-item .ct { position: absolute; top: -9px; right: -9px; background: var(--ink); color: #fff; border-radius: 999px; min-width: 24px; height: 24px; display: grid; place-items: center; font-size: 13px; font-weight: 700; border: 2px solid #fff; }
.toolbox .empty { grid-column: 1 / -1; color: var(--ink-soft); font-size: 14px; text-align: center; padding: 8px; }

.log-panel { display: flex; flex-direction: column; min-height: 200px; }
.log { flex: 1; overflow-y: auto; display: flex; flex-direction: column; gap: 4px; font-size: 14px; max-height: 240px; min-height: 100px; }
.log .m { line-height: 1.3; }
.log .m.info { color: var(--ink-soft); font-size: 13px; }
.log .m.big { font-weight: 600; }
.log .m b { font-weight: 600; }
.chat { display: flex; gap: 6px; margin-top: 8px; }
.chat input { flex: 1; min-width: 0; border: 3px solid var(--ink); border-radius: 12px; padding: 6px 10px; outline: none; }

/* ---------- Dialogs ---------- */
.dlg { border: 3px solid var(--ink); border-radius: 22px; box-shadow: 0 6px 0 var(--ink); padding: 18px; width: min(940px, calc(100vw - 24px)); max-height: calc(100dvh - 40px); }
.dlg.narrow { width: min(460px, calc(100vw - 24px)); text-align: center; }
.dlg::backdrop { background: rgba(30, 40, 60, .45); backdrop-filter: blur(2px); }
.dlg-head { display: flex; justify-content: space-between; align-items: center; margin-bottom: 12px; }
.dlg-head h2 { margin: 0; }
.x { border: 3px solid var(--ink); background: #fff; border-radius: 10px; width: 36px; height: 36px; font-weight: 700; }
.level-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(160px, 1fr)); gap: 10px; margin-top: 14px; }
.lv {
  border: 3px solid var(--ink); border-radius: 16px; background: #f3f7fb; padding: 12px; text-align: left; box-shadow: 0 3px 0 var(--ink);
  display: flex; flex-direction: column; gap: 4px; min-height: 110px;
}
.lv:hover:not(:disabled) { background: #fff6d6; }
.lv .e { font-size: 22px; }
.lv canvas { width: 100%; aspect-ratio: 12 / 7; border-radius: 10px; border: 2px solid var(--ink); background: var(--sky); }
.lv .ttl { display: flex; gap: 6px; align-items: center; }
.lv b { font-size: 16px; }
.lv .st { color: var(--yellow-d); letter-spacing: 2px; font-size: 15px; }
.lv.cur { outline: 3px solid var(--blue); }
.lv.rand { background: linear-gradient(135deg, #ffe7f0, #e3f2ff); }
.end-stars { font-size: 52px; letter-spacing: 6px; color: var(--yellow); -webkit-text-stroke: 2px var(--ink); margin: 8px 0; }
.end-stars .off { color: #dfe5ec; }
.end-actions { display: flex; gap: 10px; justify-content: center; flex-wrap: wrap; margin-top: 14px; }
.vote-list { display: flex; flex-direction: column; gap: 8px; margin-top: 12px; }
.vote-list .btn { justify-content: flex-start; }

.toast {
  position: fixed; bottom: 24px; left: 50%; transform: translate(-50%, 200%); opacity: 0; background: var(--ink); color: #fff; padding: 10px 18px;
  border-radius: 14px; font-weight: 600; transition: transform .25s, opacity .25s; z-index: 50; max-width: calc(100vw - 32px); text-align: center;
}
.toast.show { transform: translate(-50%, 0); opacity: 1; }

/* ---------- Responsive ---------- */
@media (max-width: 980px) {
  .home-inner { grid-template-columns: 1fr; padding-top: 70px; }
  .layout { grid-template-columns: 1fr; overflow-y: auto; }
  #game { height: auto; min-height: 100dvh; }
  .stage { max-height: none; }
  .side { overflow: visible; }
  .floaters .f5, .floaters .f3 { display: none; }
}
@media (max-width: 600px) {
  .modes { grid-template-columns: 1fr; }
  .home-actions { grid-template-columns: 1fr; }
  .hide-sm { display: none; }
  .topbar { padding: 8px 10px; gap: 6px; }
  .lvl-btn { font-size: 16px; }
  .badge { display: none; }
  .layout { padding: 10px; gap: 10px; }
  .tool { width: 40px; height: 40px; }
  .actionbar { justify-content: center; }
  #btnConfirm { flex: 1; }
}
