/* ===== Reset & base ===== */
*, *::before, *::after { margin: 0; padding: 0; box-sizing: border-box; }

:root {
    --bg: #101019;
    --panel: #1b1d2f;
    --line: #33375c;
    --line-strong: #6a70a8;
    --text: #f1f3ff;
    --muted: #9aa0cc;
    --accent: #8f7bff;
    --accent-soft: rgba(143, 123, 255, 0.22);
    --peer: rgba(255, 255, 255, 0.055);
    --same: rgba(143, 123, 255, 0.16);
    --bad: #ff6b78;
    --given: #dfe3ff;
}

html, body { height: 100%; overscroll-behavior: none; }

body {
    position: fixed;
    inset: 0;
    overflow: hidden;
    color: var(--text);
    font-family: 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', 'Segoe UI', sans-serif;
    background:
        radial-gradient(circle at 50% 0%, rgba(147, 129, 255, 0.16), transparent 55%),
        linear-gradient(180deg, #0a0a11, #151726);
    user-select: none;
    -webkit-user-select: none;
    -webkit-tap-highlight-color: transparent;
    touch-action: manipulation;
}

.shell {
    display: flex;
    flex-direction: column;
    height: 100%;
    min-height: 100dvh;
    max-width: 520px;
    margin: 0 auto;
    padding:
        max(6px, env(safe-area-inset-top))
        max(8px, env(safe-area-inset-right))
        max(8px, env(safe-area-inset-bottom))
        max(8px, env(safe-area-inset-left));
}

/* ===== Toolbar & HUD ===== */
.toolbar { display: flex; align-items: center; gap: 6px; flex: 0 0 auto; height: 38px; }
.toolbar-spacer { flex: 1; }

.icon-btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    min-width: 34px;
    height: 31px;
    padding: 0 10px;
    border: 1px solid var(--line);
    border-radius: 9px;
    background: var(--panel);
    color: var(--text);
    font-family: inherit;
    font-size: 0.88rem;
    line-height: 1;
    text-decoration: none;
    cursor: pointer;
    transition: transform 0.12s ease, border-color 0.12s ease;
}

.icon-btn-text { font-size: 0.76rem; font-weight: 600; }
.icon-btn:active { transform: scale(0.94); border-color: var(--accent); }

.hud { display: flex; gap: 6px; flex: 0 0 auto; margin: 5px 0; }

.hud span {
    flex: 1;
    display: flex;
    flex-direction: column;
    align-items: center;
    padding: 4px 2px;
    border-radius: 9px;
    background: rgba(255, 255, 255, 0.05);
}

.hud b { font-size: 0.9rem; font-variant-numeric: tabular-nums; }
.hud i { font-style: normal; font-size: 0.58rem; letter-spacing: 0.08em; color: var(--muted); }
.hud b.is-bad { color: var(--bad); }

/* ===== Grid ===== */
.board-area { flex: 1 1 auto; min-height: 0; display: flex; align-items: center; justify-content: center; }

.grid {
    display: grid;
    grid-template-columns: repeat(9, 1fr);
    grid-template-rows: repeat(9, 1fr);
    gap: 0;
    border: 2px solid var(--line-strong);
    border-radius: 8px;
    overflow: hidden;
    background: var(--line);
}

.cell {
    position: relative;
    display: flex;
    align-items: center;
    justify-content: center;
    background: #171a2c;
    border-right: 1px solid rgba(255, 255, 255, 0.07);
    border-bottom: 1px solid rgba(255, 255, 255, 0.07);
    font-size: var(--digit-size, 20px);
    font-weight: 600;
    font-variant-numeric: tabular-nums;
    color: var(--accent);
    cursor: pointer;
}

.cell:nth-child(9n) { border-right: 0; }
.cell:nth-child(n+73) { border-bottom: 0; }
/* thick lines between the 3x3 boxes */
.cell.box-r { border-right: 2px solid var(--line-strong); }
.cell.box-b { border-bottom: 2px solid var(--line-strong); }

.cell.is-given { color: var(--given); font-weight: 700; }
.cell.is-peer { background: #1e2138; }
.cell.is-same { background: #262a48; }
.cell.is-selected { background: var(--accent-soft); }
.cell.is-bad { color: var(--bad); }
.cell.is-flash { animation: flash 0.45s ease; }

@keyframes flash {
    0%, 100% { background: var(--accent-soft); }
    50% { background: rgba(255, 107, 120, 0.4); }
}

.notes {
    position: absolute;
    inset: 6%;
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    grid-template-rows: repeat(3, 1fr);
    font-size: var(--note-size, 8px);
    font-weight: 600;
    color: var(--muted);
    pointer-events: none;
}

.notes span { display: flex; align-items: center; justify-content: center; line-height: 1; }

/* ===== Digit pad ===== */
.pad { display: flex; gap: 4px; flex: 0 0 auto; margin-top: 8px; }

.digit {
    flex: 1;
    min-width: 0;
    height: 54px;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 1px;
    border: 1px solid var(--line);
    border-radius: 10px;
    background: var(--panel);
    color: var(--text);
    font-family: inherit;
    font-size: 1.28rem;
    font-weight: 700;
    cursor: pointer;
    transition: transform 0.1s ease, border-color 0.1s ease;
}

.digit small { font-size: 0.52rem; font-weight: 600; color: var(--muted); }
.digit:active { transform: scale(0.93); border-color: var(--accent); }
.digit.is-done { opacity: 0.32; }

/* ===== Tools ===== */
.tools { display: flex; gap: 6px; flex: 0 0 auto; margin-top: 6px; }

.tool {
    flex: 1;
    min-height: 50px;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 2px;
    border: 1px solid var(--line);
    border-radius: 11px;
    background: var(--panel);
    color: var(--text);
    font-family: inherit;
    cursor: pointer;
    transition: transform 0.1s ease, border-color 0.1s ease;
}

.tool i { font-style: normal; font-size: 1.05rem; line-height: 1; }
.tool span { font-size: 0.6rem; color: var(--muted); }
.tool:active { transform: scale(0.95); }
.tool:disabled { opacity: 0.4; }
.tool.is-on { border-color: var(--accent); background: var(--accent-soft); }
.tool.is-on span { color: var(--text); }

/* ===== Overlays ===== */
.overlay {
    position: absolute;
    inset: 0;
    display: none;
    align-items: center;
    justify-content: center;
    padding: 20px;
    background: rgba(6, 6, 14, 0.88);
    backdrop-filter: blur(5px);
    -webkit-backdrop-filter: blur(5px);
    z-index: 50;
}

.overlay.is-open { display: flex; }

.panel {
    width: 100%;
    max-width: 330px;
    display: flex;
    flex-direction: column;
    gap: 10px;
    padding: 22px 18px;
    border: 1px solid var(--line);
    border-radius: 20px;
    background: linear-gradient(180deg, #22254099, #14162699), #191b2c;
    text-align: center;
    animation: pop 0.22s ease;
}

@keyframes pop { from { opacity: 0; transform: translateY(12px) scale(0.97); } to { opacity: 1; transform: none; } }

.panel-title { font-size: 1.5rem; font-weight: 800; letter-spacing: 0.08em; color: var(--accent); }
.panel-title-lose { color: var(--bad); }
.panel-note { font-size: 0.84rem; color: var(--muted); line-height: 1.7; }

.levels { display: flex; flex-direction: column; gap: 8px; }

.level {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 13px 16px;
    border: 1px solid var(--line);
    border-radius: 13px;
    background: rgba(255, 255, 255, 0.05);
    color: var(--text);
    font-family: inherit;
    font-size: 0.95rem;
    font-weight: 700;
    cursor: pointer;
}

.level small { font-size: 0.68rem; font-weight: 500; color: var(--muted); }
.level:active { border-color: var(--accent); }
.level.is-current { border-color: var(--accent); background: var(--accent-soft); }

.btn {
    min-height: 46px;
    padding: 12px 20px;
    border: 1px solid var(--line);
    border-radius: 13px;
    background: rgba(255, 255, 255, 0.05);
    color: var(--text);
    font-family: inherit;
    font-size: 0.92rem;
    font-weight: 700;
    text-decoration: none;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
}

.btn:active { transform: scale(0.97); }
.btn-primary { border-color: transparent; background: linear-gradient(135deg, #9d8bff, #6c4ff5); color: #100a2a; }

.stat-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 7px; }
.stat { padding: 9px 6px; border-radius: 11px; background: rgba(255, 255, 255, 0.06); }
.stat b { display: block; font-size: 1.15rem; font-variant-numeric: tabular-nums; }
.stat span { font-size: 0.64rem; color: var(--muted); }

@media (max-height: 680px) {
    .hud { margin: 3px 0; }
    .digit { height: 46px; font-size: 1.12rem; }
    .tool { min-height: 44px; }
    .pad { margin-top: 5px; }
}

@media (prefers-reduced-motion: reduce) {
    .panel { animation: none; }
    .cell.is-flash { animation: none; }
}
