:root {
    --bg:           #262421;
    --bg-2:         #312e2b;
    --bg-3:         #3a3735;
    --fg:           #f0f0ee;
    --muted:        #a8a4a0;
    --accent:       #81b64c;
    --accent-dark:  #5d8c34;
    --accent-darker:#446a26;
    --light-sq:     #ebecd0;
    --dark-sq:      #779556;
    --hint:         #f7d046;
    --wrong:        #d75d5d;
    --warn:         #ec9b35;
    --solved-bg:    rgba(129, 182, 76, 0.18);
    --wrong-bg:     rgba(215, 93, 93, 0.18);
    --warn-bg:      rgba(236, 155, 53, 0.18);
}

* { box-sizing: border-box; }

html, body {
    margin: 0;
    padding: 0;
    background: var(--bg);
    color: var(--fg);
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
    font-size: 15px;
    min-height: 100vh;
    -webkit-font-smoothing: antialiased;
    -webkit-tap-highlight-color: transparent;
    -webkit-text-size-adjust: 100%;
}

body { overflow-x: hidden; }

a { color: var(--accent); }

.muted { color: var(--muted); }
.small { font-size: 0.85rem; }

/* ---- Top bar ---- */

.topbar {
    background: var(--bg-2);
    border-bottom: 1px solid var(--bg-3);
    position: sticky;
    top: 0;
    z-index: 10;
}

.topbar-inner {
    max-width: 720px;
    margin: 0 auto;
    padding: 0.65rem 0.75rem;
    display: flex;
    align-items: center;
    gap: 0.75rem;
    flex-wrap: nowrap;
    min-height: 44px;
}

.brand {
    color: var(--fg);
    text-decoration: none;
    font-weight: 700;
    font-size: 1rem;
    letter-spacing: 0.2px;
    white-space: nowrap;
}

.menu {
    flex: 1;
    display: flex;
    gap: 0.4rem;
    min-width: 0;
}

.menu a {
    color: var(--muted);
    text-decoration: none;
    padding: 0.45rem 0.7rem;
    border-radius: 6px;
    font-weight: 500;
    transition: background 0.15s, color 0.15s;
}

@media (hover: hover) {
    .menu a:hover { color: var(--fg); }
}

.menu a.active {
    color: var(--fg);
    background: var(--bg-3);
}

.who {
    color: var(--muted);
    font-size: 0.82rem;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    max-width: 7em;
}

@media (max-width: 380px) {
    .who { display: none; }
    .topbar-inner { gap: 0.5rem; }
}

/* ---- Page ---- */

.page {
    max-width: 720px;
    margin: 0 auto;
    padding: 0.75rem 0.75rem calc(0.75rem + env(safe-area-inset-bottom));
}

/* ---- Puzzle page ---- */

.puzzle-page {
    display: flex;
    flex-direction: column;
    gap: 0.6rem;
    align-items: stretch;
    max-width: 520px;
    margin: 0 auto;
    width: 100%;
}

.status-bar {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 0.5rem;
    padding: 0.5rem 0.25rem;
}

.status {
    flex: 1;
    padding: 0.5rem 0.85rem;
    border-radius: 8px;
    background: var(--bg-2);
    font-size: 0.95rem;
    font-weight: 500;
    display: flex;
    align-items: center;
    gap: 0.5rem;
}

.status::before {
    content: '';
    width: 8px;
    height: 8px;
    border-radius: 50%;
    background: var(--muted);
    flex-shrink: 0;
}

.status[data-state="ready"]::before     { background: var(--accent); }
.status[data-state="thinking"]::before  { background: var(--warn);   animation: pulse-dot 1s infinite; }
.status[data-state="wrong"]::before     { background: var(--wrong); }
.status[data-state="solved"]::before    { background: var(--accent); }
.status[data-state="solved-help"]::before{ background: var(--warn); }
.status[data-state="failed"]::before    { background: var(--wrong); }
.status[data-state="shown"]::before     { background: var(--warn); }
.status[data-state="done"]::before      { background: var(--accent); }
.status[data-state="error"]::before     { background: var(--wrong); }

.status[data-state="wrong"]      { background: var(--wrong-bg); color: var(--wrong); }
.status[data-state="solved"]     { background: var(--solved-bg); color: var(--accent); }
.status[data-state="solved-help"]{ background: var(--warn-bg); color: var(--warn); }
.status[data-state="failed"]     { background: var(--wrong-bg); color: var(--wrong); }
.status[data-state="shown"]      { background: var(--warn-bg); color: var(--warn); }

@keyframes pulse-dot {
    0%, 100% { opacity: 1; }
    50%      { opacity: 0.4; }
}

.rating-pill {
    background: var(--bg-2);
    color: var(--fg);
    font-weight: 700;
    font-size: 0.95rem;
    padding: 0.5rem 0.85rem;
    border-radius: 8px;
    min-width: 60px;
    text-align: center;
    letter-spacing: 0.5px;
}

/* ---- Board ---- */

.board {
    aspect-ratio: 1 / 1;
    width: 100%;
    background: var(--bg-2);
    border-radius: 6px;
    overflow: hidden;
    box-shadow: 0 6px 24px rgba(0, 0, 0, 0.35);
    user-select: none;
    -webkit-user-select: none;
    touch-action: manipulation;
}

.board-svg {
    display: block;
    width: 100%;
    height: 100%;
}

/* Base square fills are set as SVG attributes in board.js so iOS Safari
   renders them even when CSS var() in fill is unreliable. The rules below
   only override for highlight states. */

.board-svg .sq.selected,
.board-svg .sq.last-from,
.board-svg .sq.last-to {
    fill: #f6f669 !important;
}

.board-svg .sq.last-from,
.board-svg .sq.last-to {
    fill-opacity: 1;
    fill: #cdd26a !important;
}

.board-svg .sq.dark.last-from,
.board-svg .sq.dark.last-to { fill: #aaa23a !important; }

.board-svg .sq.dark.selected { fill: #bbcb44 !important; }
.board-svg .sq.light.selected { fill: #f5f682 !important; }

.board-svg .sq.hint {
    fill: var(--hint) !important;
    animation: pulse 1.2s ease-in-out infinite;
}

.board-svg .sq.wrong { fill: var(--wrong) !important; }

@keyframes pulse {
    0%, 100% { opacity: 1; }
    50%      { opacity: 0.55; }
}

.board-svg .sq { cursor: pointer; }

.board-svg .piece {
    font-family: 'Apple Symbols', 'Segoe UI Symbol', 'Symbola',
                 'DejaVu Sans', 'Arial Unicode MS', sans-serif;
    font-feature-settings: 'liga' off;
}

.board-svg .coord {
    fill: rgba(0, 0, 0, 0.45);
    font-family: -apple-system, sans-serif;
    font-weight: 600;
    pointer-events: none;
    user-select: none;
    -webkit-user-select: none;
}

.board-svg .piece {
    pointer-events: none;
    user-select: none;
    -webkit-user-select: none;
}

/* ---- Controls ---- */

.controls {
    display: flex;
    gap: 0.4rem;
    margin-top: 0.25rem;
}

.controls button {
    flex: 1;
    background: var(--bg-2);
    color: var(--fg);
    border: none;
    border-radius: 8px;
    padding: 0.7rem 0.4rem;
    min-height: 48px;
    font-size: 1.15rem;
    font-weight: 600;
    cursor: pointer;
    transition: background 0.15s, transform 0.05s;
    box-shadow: 0 2px 0 rgba(0, 0, 0, 0.25);
    -webkit-tap-highlight-color: transparent;
    touch-action: manipulation;
    -webkit-appearance: none;
    appearance: none;
    font-family: inherit;
    white-space: nowrap;
}

@media (hover: hover) {
    .controls button:hover:not(:disabled) {
        background: var(--bg-3);
    }

    .controls button.primary:hover:not(:disabled) {
        background: #94c95b;
    }
}

.controls button:active:not(:disabled) {
    transform: translateY(1px);
    box-shadow: 0 1px 0 rgba(0, 0, 0, 0.25);
}

.controls button:disabled {
    opacity: 0.35;
    cursor: not-allowed;
}

.controls button.primary {
    background: var(--accent);
    color: #1d2614;
    box-shadow: 0 2px 0 var(--accent-darker);
    font-size: 0.95rem;
    flex: 1.4;
}

.controls button.primary:disabled {
    background: var(--bg-2);
    color: var(--muted);
    box-shadow: 0 2px 0 rgba(0, 0, 0, 0.25);
}

/* ---- Stats page ---- */

.summary-row {
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    gap: 0.5rem;
    margin-bottom: 0.75rem;
}

@media (max-width: 520px) {
    .summary-row {
        grid-template-columns: repeat(2, 1fr);
    }
}

.card {
    background: var(--bg-2);
    border-radius: 8px;
    padding: 0.85rem 1rem;
    margin-bottom: 0.75rem;
}

.card-header {
    color: var(--muted);
    text-transform: uppercase;
    letter-spacing: 1.5px;
    font-size: 0.7rem;
    margin-bottom: 0.6rem;
    font-weight: 600;
}

.card.stat {
    text-align: center;
    padding: 0.85rem 0.5rem;
    margin-bottom: 0;
}

.stat-num {
    font-size: 1.7rem;
    color: var(--accent);
    font-weight: 700;
    line-height: 1;
}

.stat-lbl {
    color: var(--muted);
    text-transform: uppercase;
    letter-spacing: 1px;
    font-size: 0.65rem;
    margin-top: 0.4rem;
    font-weight: 600;
}

.two-col {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 0.75rem;
    margin-bottom: 0.75rem;
}

@media (max-width: 600px) {
    .two-col {
        grid-template-columns: 1fr;
    }
}

.activity {
    display: flex;
    align-items: flex-end;
    gap: 3px;
    height: 50px;
}

.activity .bar {
    flex: 1;
    background: var(--accent-darker);
    border-radius: 2px 2px 0 0;
    min-height: 4px;
    transition: background 0.15s;
}

@media (hover: hover) {
    .activity .bar:hover { background: var(--accent); }
}

.activity .bar[data-n="0"] { background: var(--bg-3); }

table.themes {
    width: 100%;
    border-collapse: collapse;
    font-size: 0.88rem;
}

table.themes th,
table.themes td {
    text-align: left;
    padding: 0.45rem 0.5rem;
    border-bottom: 1px solid var(--bg-3);
}

table.themes th {
    color: var(--muted);
    font-weight: 600;
    text-transform: uppercase;
    font-size: 0.68rem;
    letter-spacing: 1px;
}

table.themes tr:last-child td {
    border-bottom: none;
}

table.themes td:nth-child(2),
table.themes td:nth-child(3),
table.themes td:nth-child(4),
table.themes td:nth-child(5) {
    font-variant-numeric: tabular-nums;
    color: var(--accent);
}

/* ---- Error panel ---- */

.err {
    background: var(--wrong-bg);
    color: var(--wrong);
    border-radius: 8px;
    padding: 1rem 1.25rem;
}

.err h2 { margin-top: 0; }

.err code {
    background: rgba(0, 0, 0, 0.3);
    padding: 1px 6px;
    border-radius: 3px;
}
