/* ==========================================================================
   HexaGems — game-specific styles.
   Everything else comes from kt-styles. Layout glue uses kt tokens only.

   The exception to "tokens only" is the gems: they are the game's pieces,
   not interface chrome, so their colors come from the schemes below and stay
   the same in light and dark mode (kt's --kt-text / --kt-surface swap).
   ========================================================================== */

:root {
    --game-white: #ffffff;
    --game-black: #111111;
    --game-flip-duration: 180ms;
}

/* ---------- Gem color schemes ----------
   A scheme only sets two base colors; every gem shade is derived from them.
   Chosen in Settings and stored on <html data-gems="…">. The preview gems in the
   Settings dialog carry their own data-gems, so the shades are worked out per element.
   Built from kt-styles color tokens where possible. */
:root,
[data-gems] {
    --piece-light-top: var(--gem-light);
    --piece-light-bottom: color-mix(in srgb, var(--gem-light) 95%, var(--game-black));
    --piece-light-edge: color-mix(in srgb, var(--gem-light) 78%, var(--game-black));
    --piece-dark-top: color-mix(in srgb, var(--gem-dark) 88%, var(--game-white));
    --piece-dark-bottom: var(--gem-dark);
    --piece-dark-edge: color-mix(in srgb, var(--gem-dark) 70%, var(--game-black));
}
:root,
[data-gems="classic"]  { --gem-dark: #4d525a; --gem-light: #f3f4f6; }   /* slate & porcelain */
[data-gems="ocean"]    { --gem-dark: color-mix(in srgb, var(--kt-blue) 55%, var(--game-black));   --gem-light: color-mix(in srgb, var(--kt-blue) 14%, var(--game-white)); }
[data-gems="teal"]     { --gem-dark: color-mix(in srgb, var(--kt-teal) 80%, var(--game-black));   --gem-light: color-mix(in srgb, var(--kt-teal) 13%, var(--game-white)); }
[data-gems="forest"]   { --gem-dark: color-mix(in srgb, var(--kt-green) 55%, var(--game-black));  --gem-light: color-mix(in srgb, var(--kt-green) 15%, var(--game-white)); }
[data-gems="violet"]   { --gem-dark: color-mix(in srgb, var(--kt-purple) 65%, var(--game-black)); --gem-light: color-mix(in srgb, var(--kt-purple) 12%, var(--game-white)); }
[data-gems="rose"]     { --gem-dark: color-mix(in srgb, var(--kt-pink) 62%, var(--game-black));   --gem-light: color-mix(in srgb, var(--kt-pink) 11%, var(--game-white)); }
[data-gems="amber"]    { --gem-dark: color-mix(in srgb, var(--kt-orange) 48%, var(--game-black)); --gem-light: color-mix(in srgb, var(--kt-gold) 20%, var(--game-white)); }
[data-gems="contrast"] { --gem-dark: var(--game-black); --gem-light: var(--game-white); }

/* Scheme picker cards (Settings) */
.game-scheme {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: var(--kt-space-2);
    padding-right: var(--kt-space-8);   /* room for the card's check mark */
}
.game-scheme__preview { width: 64px; height: auto; }
.game-scheme__top,
.game-scheme__edge { stroke-width: 1.5; stroke-linejoin: round; }
.game-scheme__top--light  { fill: var(--piece-light-top);  stroke: var(--piece-light-top); }
.game-scheme__edge--light { fill: var(--piece-light-edge); stroke: var(--piece-light-edge); }
.game-scheme__top--dark   { fill: var(--piece-dark-top);   stroke: var(--piece-dark-top); }
.game-scheme__edge--dark  { fill: var(--piece-dark-edge);  stroke: var(--piece-dark-edge); }
.game-scheme__name { font-weight: var(--kt-weight-bold); }

/* ---------- Page layout ---------- */
.game-container {
    max-width: 640px;
}

.game-header {
    margin-bottom: var(--kt-space-3);
    flex-wrap: nowrap;
}
.game-header > .kt-cluster { flex-wrap: nowrap; flex: none; }
.game-header__actions { --kt-gap: 0; }   /* the ghost buttons' own padding already spaces the icons */

/* Icons inside buttons keep their size when the button is narrow */
.kt-btn svg { flex: none; }

.game-title {
    display: flex;
    align-items: center;
    gap: var(--kt-space-2);
    letter-spacing: var(--kt-tracking-caps);
}


/* ---------- Game information ---------- */
.game-stats {
    flex: 1 1 auto;
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: var(--kt-space-2);
}

/* Keyboard keys in How to play: body size like all other text */
#help-modal kbd { font-size: var(--kt-fs-base); }

/* Leaderboard: your own row stands out */
.kt-table tr.game-you td {
    background: var(--kt-accent-soft);
    font-weight: var(--kt-weight-bold);
}

/* Level / Moves tiles: centered */
.game-stats .kt-stat { align-items: center; text-align: center; }

.game-stats .kt-stat__sub {
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    max-width: 100%;
}


/* ---------- Board ----------
   An SVG tray of hexagonal pieces drawn by game.js. Each .game-cell is:
     .game-edge   the piece's thickness, a hexagon peeking out below the top
     .game-top    the bevelled top face (.game-face + .game-facet)
   ------------------------------------------------------------------------- */
.game-board-wrap {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: var(--kt-space-3);
}

.game-board {
    display: block;
    width: 100%;
    max-width: 600px;
    height: auto;
    max-height: max(280px, 100svh - 430px);
    padding: var(--kt-space-3);
    /* A neutral tray between the two piece colors, so neither looks like an empty slot */
    background: color-mix(in srgb, var(--kt-border) 85%, var(--kt-surface));
    border-radius: var(--kt-radius-xl);
    overflow: visible;
    touch-action: manipulation;
    user-select: none;
    -webkit-user-select: none;
}
:root[data-theme="dark"] .game-board {
    background: color-mix(in srgb, var(--kt-border) 60%, var(--kt-surface));
}

.game-stop--light-top    { stop-color: var(--piece-light-top); }
.game-stop--light-bottom { stop-color: var(--piece-light-bottom); }
.game-stop--dark-top     { stop-color: var(--piece-dark-top); }
.game-stop--dark-bottom  { stop-color: var(--piece-dark-bottom); }

.game-cell {
    cursor: pointer;
    outline: none;
    transform-box: fill-box;
    transform-origin: center;
    -webkit-tap-highlight-color: transparent;
}

/* Same-color strokes with round joins give the hexagons soft corners */
.game-face,
.game-edge {
    stroke-width: 1.6;
    stroke-linejoin: round;
    /* The color swaps at the midpoint of the flip, while the piece is edge-on */
    transition: fill 0s linear calc(var(--game-flip-duration) / 2),
                stroke 0s linear calc(var(--game-flip-duration) / 2);
}
.game-face { fill: url(#bw-face-light); stroke: url(#bw-face-light); }
.game-edge { fill: var(--piece-light-edge); stroke: var(--piece-light-edge); }
.game-cell.is-black .game-face { fill: url(#bw-face-dark); stroke: url(#bw-face-dark); }
.game-cell.is-black .game-edge { fill: var(--piece-dark-edge); stroke: var(--piece-dark-edge); }

/* Bevel: six sloped facets around the flat top, lit from the upper left.
   They are translucent white / black over the face, so they work on both colors. */
.game-facet { pointer-events: none; stroke: none; }
.game-facet--lit   { fill: var(--game-white); fill-opacity: calc(var(--facet) * 0.42); }
.game-facet--shade { fill: var(--game-black); fill-opacity: calc(var(--facet) * 0.2); }

/* Lift: the top face rises, showing more of the edge underneath */
.game-top {
    transition: transform 160ms cubic-bezier(0.34, 1.56, 0.64, 1);
}

/* Flip: the whole piece turns over like a coin.
   Two identical animations: alternating between them restarts it without a reflow. */
.game-cell.flip-a { animation: game-flip-a var(--game-flip-duration) ease-in-out; }
.game-cell.flip-b { animation: game-flip-b var(--game-flip-duration) ease-in-out; }
@keyframes game-flip-a { 50% { transform: scaleX(0.04); } }
@keyframes game-flip-b { 50% { transform: scaleX(0.04); } }

/* ---------- Preview (mouse hover, keyboard focus, finger down) ----------
   The pieces a click would flip rise together;
   the piece you are about to click rises the highest. */
.game-cell.is-preview .game-top        { transform: translateY(-1.4px); }
.game-cell.is-preview-center .game-top { transform: translateY(-2.6px); }

/* Press: the piece is pushed down into the tray */
.game-cell:active .game-top,
.game-cell.is-preview-center:active .game-top {
    transform: translateY(0.9px);
    transition-duration: 60ms;
}

.game-cell:focus-visible { box-shadow: none; }

/* ---------- New level: pieces drop in from the middle outwards ---------- */
.game-board.is-entering .game-top {
    animation: game-enter 360ms cubic-bezier(0.34, 1.4, 0.64, 1) both;
    animation-delay: calc(var(--enter, 0) * 35ms);
}
.game-board.is-entering .game-edge {
    animation: game-enter-edge 360ms ease both;
    animation-delay: calc(var(--enter, 0) * 35ms);
}
@keyframes game-enter {
    from { transform: translateY(-6px); opacity: 0; }
}
@keyframes game-enter-edge {
    from { opacity: 0; }
}

/* ---------- Solved: the pieces hop in a wave from the winning move ---------- */
.game-board.is-solved .game-cell { cursor: default; }
.game-board.is-celebrating .game-top {
    animation: game-hop 420ms cubic-bezier(0.34, 1.4, 0.64, 1) both;
    animation-delay: calc(var(--wave, 0) * 45ms);
}
@keyframes game-hop {
    40% { transform: translateY(-3.5px); }
    100% { transform: none; }
}

/* ---------- Controls ---------- */
.game-controls {
    padding-top: var(--kt-space-4);
    padding-bottom: var(--kt-space-4);
}

.game-actions {
    display: grid;
    grid-template-columns: repeat(3, minmax(0, 1fr));
    gap: var(--kt-space-2);
}



/* ---------- Helpers ---------- */
.game-sr-only {
    position: absolute;
    width: 1px;
    height: 1px;
    margin: -1px;
    padding: 0;
    overflow: hidden;
    clip: rect(0 0 0 0);
    white-space: nowrap;
    border: 0;
}


/* ---------- Small screens ---------- */
@media (max-width: 520px) {
    .kt-page-header .game-title { font-size: var(--kt-fs-lg); white-space: nowrap; }
    #reset-button > span { display: none; }   /* icon only; the button keeps its aria-label */
    #reset-button { width: var(--kt-control-h); padding: 0; }
    .game-board-wrap { padding-left: var(--kt-space-2); padding-right: var(--kt-space-2); }
    .game-board { padding: var(--kt-space-2); }
}

/* Very narrow phones: the three action buttons drop their icons so the labels fit */
@media (max-width: 380px) {
    .game-actions .kt-btn { padding: 0 var(--kt-space-2); }
    .game-actions .kt-btn svg { display: none; }
}

@media (prefers-reduced-motion: reduce) {
    .game-face,
    .game-edge { transition: fill var(--kt-transition-fast), stroke var(--kt-transition-fast); }
    .game-cell.flip-a,
    .game-cell.flip-b,
    .game-board.is-entering .game-top,
    .game-board.is-entering .game-edge,
    .game-board.is-celebrating .game-top { animation: none; }
    .game-top { transition: none; }
}

/* ---------- Stats page (stats.html) ---------- */
.stats-container { max-width: 960px; }
