:root {
  --bg: #0a0f1d;
  --panel: #141c2f;
  --panel-2: #1b2640;
  --line: #2b3857;
  --text: #edf3ff;
  --muted: #93a4c5;
  --accent: #54d7ff;
  --accent-2: #7a78ff;
  --danger: #ff657d;
  --success: #62e6a7;
}
* { box-sizing: border-box; }
html, body { margin: 0; min-height: 100%; }
body {
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, "Segoe UI", sans-serif;
  color: var(--text);
  background:
    radial-gradient(circle at 15% 10%, rgba(84,215,255,.14), transparent 27rem),
    radial-gradient(circle at 90% 20%, rgba(122,120,255,.13), transparent 28rem),
    var(--bg);
}
button, input, select { font: inherit; }
button { cursor: pointer; }
.app-shell { width: min(1180px, calc(100% - 28px)); margin: 0 auto; padding: 22px 0 30px; }
.topbar { display: flex; align-items: center; justify-content: space-between; gap: 16px; margin-bottom: 18px; }
h1 { margin: 0; font-size: clamp(24px, 4vw, 38px); letter-spacing: .06em; }
h1 span { color: var(--accent); }
.topbar p { margin: 4px 0 0; color: var(--muted); }
.top-actions { display: flex; flex-wrap: wrap; gap: 8px; justify-content: flex-end; }
.btn, .icon-btn {
  border: 1px solid transparent; border-radius: 10px; min-height: 42px; padding: 9px 15px;
  font-weight: 750; color: var(--text); transition: .15s ease;
}
.btn.primary { background: linear-gradient(135deg, #168ab5, #6766d9); }
.btn.secondary { background: var(--panel-2); border-color: var(--line); }
.btn.danger { background: rgba(255,101,125,.12); border-color: rgba(255,101,125,.5); color: #ff9aaa; }
.btn:disabled { opacity: .45; cursor: not-allowed; }
.btn:hover:not(:disabled), .icon-btn:hover { filter: brightness(1.12); transform: translateY(-1px); }
.game-layout { display: grid; grid-template-columns: 180px minmax(260px, 1fr) 200px; align-items: start; gap: 18px; }
.panel { background: rgba(20,28,47,.92); border: 1px solid var(--line); border-radius: 16px; padding: 15px; }
.stats-panel { display: grid; gap: 10px; }
.stat { border-bottom: 1px solid var(--line); padding: 5px 0 12px; }
.stat span { display: block; font-size: 11px; font-weight: 800; letter-spacing: .13em; color: var(--muted); }
.stat strong { display: block; font-size: 29px; line-height: 1.15; margin-top: 4px; }
.panel-section { margin-top: 18px; }
.panel-section h2 { margin: 0 0 8px; font-size: 14px; text-transform: uppercase; letter-spacing: .08em; color: var(--muted); }
.panel-section p { margin: 6px 0; }
.controls-help p { display: flex; align-items: center; gap: 5px; font-size: 12px; color: var(--muted); }
kbd { background: #0b1222; border: 1px solid var(--line); border-bottom-width: 2px; padding: 2px 6px; border-radius: 5px; color: var(--text); }
.board-wrap {
  position: relative; display: grid; place-items: center; min-width: 0; outline: none;
  background: rgba(8,13,25,.75); border: 1px solid var(--line); border-radius: 16px; padding: 14px;
}
.board-wrap:focus-visible { box-shadow: 0 0 0 3px rgba(84,215,255,.22); }
#gameCanvas { display: block; width: auto; max-width: 100%; border-radius: 7px; background: #050812; image-rendering: crisp-edges; }
.game-overlay { position: absolute; inset: 14px; display: none; place-items: center; background: rgba(5,8,18,.72); backdrop-filter: blur(2px); border-radius: 8px; }
.game-overlay.visible { display: grid; }
.overlay-card { width: min(330px, calc(100% - 24px)); text-align: center; padding: 24px; background: rgba(20,28,47,.97); border: 1px solid var(--line); border-radius: 16px; }
.overlay-card h2 { margin: 0 0 8px; font-size: 25px; }
.overlay-card p { margin: 0 0 18px; color: var(--muted); }
.next-box { text-align: center; margin-top: 0; }
#nextCanvas { width: 150px; height: 150px; max-width: 100%; background: #090e1b; border: 1px solid var(--line); border-radius: 10px; }
#nextName { display: block; margin-top: 5px; font-size: 13px; color: var(--muted); }
.piece-badges { display: flex; flex-wrap: wrap; gap: 5px; }
.piece-badge { border: 1px solid var(--line); border-radius: 7px; padding: 4px 7px; font-size: 11px; background: #0b1222; }
.mobile-controls { display: none; grid-template-columns: repeat(3, 1fr); gap: 7px; margin-top: 18px; }
.mobile-controls button { min-height: 47px; border: 1px solid var(--line); color: var(--text); background: var(--panel-2); border-radius: 9px; font-size: 22px; }
.mobile-controls button:nth-child(4) { grid-column: 1 / 3; }

.dialog { width: min(900px, calc(100% - 24px)); max-height: calc(100% - 30px); padding: 0; color: var(--text); background: #10182a; border: 1px solid var(--line); border-radius: 18px; }
.dialog::backdrop { background: rgba(2,5,12,.78); backdrop-filter: blur(3px); }
.dialog form { padding: 20px; }
.dialog-head { display: flex; align-items: start; justify-content: space-between; gap: 16px; border-bottom: 1px solid var(--line); padding-bottom: 15px; }
.dialog h2 { margin: 0; }
.dialog-head p, .section-title-row p { margin: 4px 0 0; color: var(--muted); font-size: 13px; }
.icon-btn { padding: 0; width: 42px; background: var(--panel-2); border-color: var(--line); font-size: 25px; }
.settings-grid { display: grid; gap: 14px; margin-top: 16px; }
.settings-card { background: var(--panel); border: 1px solid var(--line); border-radius: 13px; padding: 15px; }
.settings-card h3, .piece-editor-layout h3 { margin: 0 0 11px; font-size: 16px; }
.field-row { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 12px; }
label { display: grid; gap: 6px; font-weight: 700; font-size: 13px; color: #ccd8ee; }
input, select { width: 100%; min-height: 42px; border: 1px solid var(--line); border-radius: 8px; padding: 8px 10px; color: var(--text); background: #0c1323; outline: none; }
input:focus, select:focus { border-color: var(--accent); box-shadow: 0 0 0 3px rgba(84,215,255,.12); }
input[type="color"] { padding: 4px; }
small, .hint { color: var(--muted); font-weight: 400; }
.hint { font-size: 12px; margin: 9px 0 0; }
.section-title-row { display: flex; align-items: center; justify-content: space-between; gap: 15px; margin-bottom: 12px; }
.piece-list { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 8px; }
.piece-item { display: grid; grid-template-columns: auto 1fr auto; align-items: center; gap: 9px; min-width: 0; padding: 8px; border: 1px solid var(--line); border-radius: 10px; background: #0d1424; }
.piece-item input[type="checkbox"] { width: 19px; min-height: 19px; accent-color: var(--accent); }
.piece-preview { display: grid; grid-template-columns: repeat(var(--cols), 8px); grid-auto-rows: 8px; gap: 1px; }
.preview-cell { width: 8px; height: 8px; border-radius: 1px; }
.piece-meta { min-width: 0; }
.piece-meta strong { display: block; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.piece-meta span { color: var(--muted); font-size: 11px; }
.delete-piece { width: 32px; min-height: 32px; padding: 0; border-radius: 7px; border: 1px solid rgba(255,101,125,.4); background: rgba(255,101,125,.1); color: #ff9aaa; }
.dialog-actions { display: flex; align-items: center; justify-content: flex-end; flex-wrap: wrap; gap: 8px; border-top: 1px solid var(--line); padding-top: 15px; margin-top: 16px; }
.form-message { margin-right: auto; color: #ffadbb; font-size: 13px; }
.piece-editor-layout { display: grid; grid-template-columns: 230px 1fr; gap: 25px; margin-top: 18px; }
.editor-options { display: grid; align-content: start; gap: 12px; }
.editor-tools { display: grid; grid-template-columns: 1fr 1fr; gap: 7px; }
.piece-rules { color: var(--muted); font-size: 12px; }
.piece-rules ul { padding-left: 18px; line-height: 1.55; }
.piece-editor-grid { --size: 4; display: grid; grid-template-columns: repeat(var(--size), minmax(38px, 58px)); gap: 6px; justify-content: start; touch-action: manipulation; }
.editor-cell { aspect-ratio: 1; border: 1px solid #3b496a; border-radius: 7px; background: #0a1120; }
.editor-cell.active { border-color: rgba(255,255,255,.7); box-shadow: inset 0 0 0 2px rgba(255,255,255,.12); }

@media (max-width: 900px) {
  .game-layout { grid-template-columns: 135px minmax(240px, 1fr) 160px; gap: 10px; }
  .panel { padding: 10px; }
  .controls-help { display: none; }
  #nextCanvas { width: 120px; height: 120px; }
  .mobile-controls { display: grid; }
}
@media (max-width: 680px) {
  .app-shell { width: min(100% - 16px, 560px); padding-top: 12px; }
  .topbar { align-items: flex-start; }
  .topbar p { display: none; }
  .top-actions { max-width: 190px; }
  .game-layout { grid-template-columns: 1fr; }
  .stats-panel { grid-template-columns: repeat(4, 1fr); order: 2; }
  .stat { text-align: center; border-bottom: 0; padding: 5px; }
  .stat strong { font-size: 20px; }
  .stats-panel .panel-section { display: none; }
  .board-wrap { order: 1; }
  .right-panel { order: 3; display: grid; grid-template-columns: 150px 1fr; gap: 10px; }
  .next-box { margin: 0; }
  .right-panel > .panel-section:nth-child(2) { display: none; }
  .mobile-controls { margin: 0; align-self: center; }
  .piece-list { grid-template-columns: 1fr; }
  .piece-editor-layout { grid-template-columns: 1fr; }
  .field-row { grid-template-columns: 1fr; }
  .section-title-row { align-items: flex-start; }
}
@media (max-width: 430px) {
  .topbar { display: grid; }
  .top-actions { max-width: none; justify-content: stretch; }
  .top-actions .btn { flex: 1; padding-inline: 8px; font-size: 12px; }
  .right-panel { grid-template-columns: 118px 1fr; }
  #nextCanvas { width: 100px; height: 100px; }
  .piece-editor-grid { grid-template-columns: repeat(var(--size), minmax(32px, 1fr)); }
}
