/* =============================================================================
   Jabble — Words With Friends aesthetic: bright, rounded, friendly, casual.
   Bundled Nunito (OFL) for all UI chrome; system gothic for kana on tiles.
   ============================================================================= */

@font-face {
    font-family: 'Nunito';
    src: url('fonts/Nunito.ttf') format('truetype');
    font-weight: 200 1000;
    font-style: normal;
    font-display: swap;
}

:root {
    --bg-top: #1b3a4b;
    --bg-bot: #0f2833;
    --panel: #ffffff;
    --board: #eef2ee;
    --ink: #22343a;
    --ink-soft: #5a6f77;
    --teal: #2ca6a6;
    --coral: #e0574a;
    --green: #5bbf5a;
    --green-dk: #46a247;
    --orange: #f0a04b;
    --cream: #f6ecd4;
    --cream-dk: #ece0c0;
    --radius: 16px;
    --shadow: 0 10px 30px rgba(6, 22, 28, 0.28);
    --shadow-sm: 0 3px 10px rgba(6, 22, 28, 0.18);
}

* { box-sizing: border-box; }

html, body {
    margin: 0;
    padding: 0;
    min-height: 100%;
}

body {
    font-family: 'Nunito', system-ui, -apple-system, 'Segoe UI', sans-serif;
    color: var(--ink);
    background: linear-gradient(160deg, var(--bg-top) 0%, var(--bg-bot) 100%);
    background-attachment: fixed;
    min-height: 100vh;
    display: flex;
    justify-content: center;
    -webkit-font-smoothing: antialiased;
    text-rendering: optimizeLegibility;
}

/* --- App column ---------------------------------------------------------- */
#app {
    width: 100%;
    max-width: 560px;
    margin: 0 auto;
    padding: 16px 16px 28px;
    display: flex;
    flex-direction: column;
    gap: 14px;
}

#topbar {
    display: flex;
    align-items: center;
    justify-content: space-between;
}

.wordmark {
    font-weight: 900;
    font-size: 30px;
    letter-spacing: -0.02em;
    color: #ffffff;
    text-shadow: 0 2px 0 rgba(0, 0, 0, 0.18);
}
.wordmark .dot { color: var(--orange); }
.start-wordmark { font-size: 52px; margin-bottom: 6px; }

/* --- Buttons (friendly pills) ------------------------------------------- */
.btn {
    font-family: inherit;
    font-weight: 800;
    font-size: 16px;
    color: #fff;
    border: none;
    border-radius: 999px;
    padding: 12px 22px;
    cursor: pointer;
    box-shadow: var(--shadow-sm);
    transition: transform 0.08s ease, filter 0.15s ease, box-shadow 0.15s ease;
    -webkit-tap-highlight-color: transparent;
}
.btn:hover { filter: brightness(1.05); }
.btn:active { transform: translateY(1px) scale(0.98); }
.btn:disabled { opacity: 0.45; cursor: default; box-shadow: none; filter: none; transform: none; }

.btn-primary { background: linear-gradient(180deg, #6bcf67, var(--green-dk)); }
.btn-danger  { background: linear-gradient(180deg, #ee7266, var(--coral)); }
.btn-neutral { background: linear-gradient(180deg, #6d8891, #55707a); }
.btn-connect { background: linear-gradient(180deg, #45c3c3, #23928f); }

.btn-sm { font-size: 13px; padding: 8px 14px; }
.btn-lg { font-size: 19px; padding: 16px 22px; }
.btn-block { width: 100%; }

/* --- Score bar ----------------------------------------------------------- */
#player-info-panel {
    display: flex;
    align-items: stretch;
    gap: 10px;
}
.pcard {
    flex: 1;
    display: flex;
    align-items: center;
    gap: 10px;
    background: var(--panel);
    border-radius: var(--radius);
    padding: 10px 12px;
    box-shadow: var(--shadow-sm);
    border: 2px solid transparent;
    transition: transform 0.15s ease, box-shadow 0.15s ease, border-color 0.15s ease;
}
.pcard.active {
    border-color: var(--green);
    box-shadow: 0 8px 22px rgba(91, 191, 90, 0.35);
    transform: translateY(-2px);
}
.pcard.active.is-cpu {
    border-color: var(--coral);
    box-shadow: 0 8px 22px rgba(224, 87, 74, 0.35);
}
.pcard-avatar {
    width: 42px;
    height: 42px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    color: #fff;
    font-weight: 900;
    font-size: 16px;
    flex-shrink: 0;
    box-shadow: inset 0 -2px 4px rgba(0, 0, 0, 0.18);
}
.pcard-meta { min-width: 0; }
.pcard-name {
    font-weight: 800;
    font-size: 13px;
    color: var(--ink-soft);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}
.pcard-score { font-weight: 900; font-size: 26px; line-height: 1; color: var(--ink); }
.pcard-vs {
    align-self: center;
    font-weight: 900;
    font-size: 13px;
    color: rgba(255, 255, 255, 0.6);
}

/* --- Status / turn indicator -------------------------------------------- */
#status-bar {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: 8px;
    justify-content: center;
}
.turn-pill {
    font-weight: 900;
    font-size: 14px;
    color: #fff;
    padding: 6px 16px;
    border-radius: 999px;
    box-shadow: var(--shadow-sm);
}
.turn-pill.is-you { background: linear-gradient(180deg, #38b4b4, var(--teal)); }
.turn-pill.is-cpu { background: linear-gradient(180deg, #ee7266, var(--coral)); }
.status-round, .status-phase, .status-mastery {
    font-size: 12px;
    font-weight: 700;
    color: rgba(255, 255, 255, 0.78);
    background: rgba(255, 255, 255, 0.08);
    padding: 5px 10px;
    border-radius: 999px;
}
.status-phase.unlocked { color: #ffe6c2; background: rgba(240, 160, 75, 0.22); }
.status-mastery.on { color: #cdeecb; background: rgba(91, 191, 90, 0.2); }

/* --- Board --------------------------------------------------------------- */
#game-area {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 14px;
}
#game-board-container {
    background: var(--board);
    border-radius: 20px;
    box-shadow: var(--shadow), inset 0 0 0 3px rgba(255, 255, 255, 0.5);
    display: flex;
    align-items: center;
    justify-content: center;
    /* width/height set by resizeCanvas() */
}
#game-board {
    border-radius: 10px;
    display: block;
    touch-action: manipulation;
}

/* --- Controls: rack + action pills -------------------------------------- */
#controls-container {
    width: 100%;
    max-width: 560px;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 10px;
}
#convert-hint {
    order: 2;
    min-height: 16px;
    font-size: 13px;
    font-weight: 700;
    color: rgba(255, 255, 255, 0.72);
    text-align: center;
}
#input-rack-container {
    order: 1;
    width: 100%;
    display: flex;
    align-items: center;
    gap: 8px;
    background: linear-gradient(180deg, #caa46a, #b98f52);
    border-radius: 14px;
    padding: 8px 10px;
    box-shadow: var(--shadow-sm), inset 0 2px 6px rgba(0, 0, 0, 0.22);
    min-height: 56px;
    overflow-x: auto;
}
.tile {
    background: linear-gradient(180deg, var(--cream), var(--cream-dk));
    border: 1px solid #e0d0a6;
    border-radius: 10px;
    display: flex;
    justify-content: center;
    align-items: center;
    font-weight: 900;
    color: var(--ink);
    cursor: grab;
    user-select: none;
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.25);
    font-family: 'Hiragino Kaku Gothic ProN', 'Hiragino Sans', 'Yu Gothic', 'Noto Sans JP', sans-serif;
    flex-shrink: 0;
    animation: tile-pop 0.18s ease;
}
.tile.dragging { opacity: 0.5; }
#input-area {
    flex: 1 1 60px;
    min-width: 60px;
    outline: none;
    font-size: 28px;
    font-weight: 800;
    color: #fff;
    caret-color: #fff;
    padding: 2px 4px;
}
#input-area:empty::before {
    content: 'type romaji…';
    color: rgba(255, 255, 255, 0.55);
    font-size: 15px;
    font-weight: 700;
}
#input-area.grid-typing-active { color: transparent; caret-color: transparent; }
#input-area.grid-typing-active:empty::before { content: ''; }

.button-group {
    order: 3;
    display: flex;
    flex-wrap: wrap;
    gap: 10px;
    justify-content: center;
}

/* --- Move history -------------------------------------------------------- */
#history {
    background: var(--panel);
    border-radius: var(--radius);
    padding: 12px 14px;
    box-shadow: var(--shadow-sm);
}
.history-head {
    font-size: 12px;
    font-weight: 900;
    text-transform: uppercase;
    letter-spacing: 0.06em;
    color: var(--ink-soft);
    margin-bottom: 8px;
}
#event-log {
    max-height: 150px;
    overflow-y: auto;
    display: flex;
    flex-direction: column;
    gap: 4px;
}
.log-line {
    font-size: 13px;
    font-weight: 600;
    color: #445056;
    line-height: 1.4;
    padding: 5px 10px;
    background: #f2f5f2;
    border-radius: 8px;
    border-left: 3px solid var(--teal);
}
.log-line:first-child { border-left-color: var(--green); background: #eef7ee; }

/* --- Ad slot (subtle) ---------------------------------------------------- */
.ad-wrap { display: flex; justify-content: center; }
.ad-placeholder {
    border: 1px dashed rgba(255, 255, 255, 0.25);
    border-radius: 12px;
    padding: 14px 12px;
    text-align: center;
    color: rgba(255, 255, 255, 0.5);
    width: 100%;
}
.ad-tag {
    display: inline-block;
    font-size: 10px;
    font-weight: 900;
    letter-spacing: 0.12em;
    color: rgba(255, 255, 255, 0.5);
    border: 1px solid rgba(255, 255, 255, 0.25);
    border-radius: 4px;
    padding: 1px 5px;
    margin-bottom: 6px;
}
.ad-copy { display: block; font-size: 11px; }

/* --- Challenge modal ----------------------------------------------------- */
#challenge-modal {
    position: fixed;
    inset: 0;
    background: rgba(9, 26, 33, 0.6);
    backdrop-filter: blur(3px);
    display: none;
    align-items: center;
    justify-content: center;
    z-index: 100;
    padding: 20px;
}
#challenge-modal.open { display: flex; }
.challenge-box {
    background: var(--panel);
    border-radius: 22px;
    padding: 26px 24px;
    max-width: 400px;
    width: 100%;
    box-shadow: 0 24px 60px rgba(0, 0, 0, 0.4);
    text-align: center;
    animation: modal-in 0.28s cubic-bezier(0.2, 0.9, 0.3, 1.2);
}
.challenge-badge {
    display: inline-block;
    font-size: 11px;
    font-weight: 900;
    letter-spacing: 0.1em;
    text-transform: uppercase;
    color: #fff;
    background: linear-gradient(180deg, #6bcf67, var(--green-dk));
    padding: 4px 12px;
    border-radius: 999px;
    margin-bottom: 12px;
}
.challenge-title {
    font-size: 14px;
    font-weight: 800;
    color: var(--ink-soft);
    margin-bottom: 4px;
}
.challenge-prompt { font-size: 23px; font-weight: 900; color: var(--ink); margin-bottom: 18px; }
.challenge-choices { display: flex; flex-direction: column; gap: 10px; }
.challenge-choice {
    font-family: inherit;
    font-weight: 800;
    font-size: 16px;
    color: var(--ink);
    background: #eef2f3;
    border: 2px solid #e2e8ea;
    border-radius: 14px;
    padding: 14px;
    cursor: pointer;
    transition: transform 0.08s ease, background 0.15s ease, border-color 0.15s ease;
}
.challenge-choice:hover { background: #e4edee; border-color: var(--teal); }
.challenge-choice:active { transform: scale(0.98); }
.challenge-choice.locked { cursor: default; }
.challenge-choice.locked:hover { background: #eef2f3; border-color: #e2e8ea; }
.challenge-choice.correct { background: #e4f6e2; border-color: var(--green); color: var(--green-dk); }
.challenge-choice.incorrect { background: #fbe6e3; border-color: var(--coral); color: #c23f33; }
.challenge-note {
    margin-top: 16px;
    display: flex;
    gap: 8px;
    justify-content: center;
}
.pill-pass, .pill-fail {
    font-size: 12px;
    font-weight: 800;
    padding: 4px 12px;
    border-radius: 999px;
}
.pill-pass { color: var(--green-dk); background: #e4f6e2; }
.pill-fail { color: #c23f33; background: #fbe6e3; }

/* --- Start screen -------------------------------------------------------- */
#start-screen {
    position: fixed;
    inset: 0;
    background: linear-gradient(160deg, var(--bg-top), var(--bg-bot));
    display: none;
    align-items: center;
    justify-content: center;
    z-index: 200;
    padding: 20px;
}
#start-screen.open { display: flex; }
.start-card {
    background: var(--panel);
    border-radius: 26px;
    padding: 34px 28px;
    max-width: 400px;
    width: 100%;
    text-align: center;
    box-shadow: 0 28px 70px rgba(0, 0, 0, 0.45);
    animation: modal-in 0.3s cubic-bezier(0.2, 0.9, 0.3, 1.2);
}
.start-wordmark { color: var(--teal); text-shadow: none; }
.start-tagline { font-size: 15px; font-weight: 600; color: var(--ink-soft); margin: 0 0 24px; line-height: 1.5; }
.start-modes { display: flex; flex-direction: column; gap: 12px; margin-bottom: 18px; }
.start-note { font-size: 12px; font-weight: 600; color: #9aa6ab; margin: 10px 0 0; }

/* --- Motion -------------------------------------------------------------- */
@keyframes modal-in {
    from { opacity: 0; transform: translateY(16px) scale(0.96); }
    to   { opacity: 1; transform: translateY(0) scale(1); }
}
@keyframes tile-pop {
    from { transform: scale(0.7); }
    to   { transform: scale(1); }
}
@keyframes score-bump {
    0%   { transform: scale(1); }
    40%  { transform: scale(1.35); color: var(--green-dk); }
    100% { transform: scale(1); }
}
.pcard-score.bump { animation: score-bump 0.45s ease; }

@keyframes card-pulse {
    0%, 100% { box-shadow: 0 8px 22px rgba(91, 191, 90, 0.35); }
    50%      { box-shadow: 0 8px 30px rgba(91, 191, 90, 0.6); }
}
.pcard.active { animation: card-pulse 2s ease-in-out infinite; }
.pcard.active.is-cpu { animation: none; }

@media (prefers-reduced-motion: reduce) {
    * { animation-duration: 0.001ms !important; animation-iteration-count: 1 !important; transition-duration: 0.001ms !important; }
}

/* --- Wide screens: give the board a touch more room --------------------- */
@media (min-width: 720px) {
    #app { max-width: 600px; }
}

/* --- Topbar actions (mute + connect) + audio credit --------------------- */
.topbar-actions { display: flex; align-items: center; gap: 8px; }
#mute-btn { padding: 6px 10px; line-height: 1; }
#credits {
    text-align: center;
    font-size: 11px;
    color: rgba(255, 255, 255, 0.45);
    padding: 10px 0 4px;
    letter-spacing: 0.02em;
}
