:root {
    --jewel-bg-top: #fffbfd;
    --jewel-bg-bottom: #f9eef4;
    --jewel-surface: rgba(255, 255, 255, 0.88);
    --jewel-ink: #3a2632;
    --jewel-muted: #8d7180;
    --jewel-gold: #d6ad72;
    --jewel-gold-light: #f5e3c0;
    --jewel-gold-deep: #a67a40;
    --jewel-rose: #df6992;
    --jewel-border: rgba(232, 202, 216, 0.75);
}

*,
*::before,
*::after {
    box-sizing: border-box;
}

html,
body {
    margin: 0;
    padding: 0;
    background: linear-gradient(180deg, var(--jewel-bg-top) 0%, var(--jewel-bg-bottom) 100%);
    color: var(--jewel-ink);
    font-family: -apple-system, BlinkMacSystemFont, "Hiragino Sans", "Hiragino Kaku Gothic ProN", "Noto Sans JP", sans-serif;
    -webkit-font-smoothing: antialiased;
    overscroll-behavior-y: contain;
    user-select: none;
    -webkit-user-select: none;
}

body {
    height: 100dvh;
    min-height: 100dvh;
    display: flex;
    flex-direction: column;
}

.jewel-shell {
    flex: 1;
    min-height: 0;
    width: 100%;
    margin: 0 auto;
    padding: 2px 4px calc(4px + env(safe-area-inset-bottom, 0px));
    display: flex;
    flex-direction: column;
}

.jewel-stage {
    display: grid;
    grid-template-rows: auto minmax(0, 1fr);
    gap: 3px;
    flex: 1;
    min-height: 0;
}

/* Three compact columns leave the remaining viewport height to the board. */
.top-bar {
    display: grid;
    grid-template-columns: 66px minmax(0, 1fr) 90px;
    grid-template-rows: 66px;
    gap: 5px;
    background: var(--jewel-surface);
    border: 1px solid var(--jewel-border);
    border-radius: 16px;
    padding: 4px;
    box-shadow: 0 4px 14px rgba(158, 98, 125, 0.06);
}

.partner-bar { display: contents; }
.partner-copy { grid-column: 2; min-width: 0; }
.partner-heading { display: flex; align-items: center; justify-content: space-between; gap: 3px; margin-bottom: 2px; }
.partner-name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: 10px; font-weight: 800; color: var(--partner-accent, var(--jewel-rose)); }
.partner-avatar-wrap {
    appearance: none;
    border: 1px solid var(--jewel-gold-light);
    grid-column: 1;
    grid-row: 1;
    position: relative;
    width: 66px;
    height: 66px;
    border-radius: 14px;
    padding: 0 0 11px;
    background: radial-gradient(ellipse at 50% 70%, #fbe0eb, #fff9ee 72%);
    cursor: pointer;
    -webkit-tap-highlight-color: transparent;
}
.partner-avatar-wrap img {
    width: 100%;
    height: 100%;
    object-fit: contain;
    display: block;
    transform-origin: 50% 90%;
}
.partner-avatar-wrap.is-speaking img { animation: avatarBounce .42s ease; }
.partner-jewel { position: absolute; bottom: 12px; right: 0; width: 35px; height: 35px; filter: drop-shadow(0 2px 3px #d5ab7060); pointer-events: none; }
.partner-moment { position: absolute; bottom: 1px; left: 0; width: 100%; font-size: 8px; font-weight: 800; color: var(--jewel-gold-deep); }
.partner-bar[data-reaction="highRank"] .partner-avatar-wrap,
.partner-bar[data-reaction="best"] .partner-avatar-wrap,
.partner-bar[data-reaction="promise"] .partner-avatar-wrap { box-shadow: inset 0 0 12px #f5d48790; }
.partner-bar[data-reaction="chain"] .partner-jewel,
.partner-bar[data-reaction="highRank"] .partner-jewel,
.partner-bar[data-reaction="best"] .partner-jewel,
.partner-bar[data-reaction="promise"] .partner-jewel { animation: jewelCelebrate .65s ease; }
.partner-bar[data-reaction="danger"] .partner-avatar-wrap { border-color: var(--jewel-rose); }
button:focus-visible { outline: 2px solid var(--jewel-rose); outline-offset: 2px; }

.partner-line {
    flex: 1;
    min-width: 0;
    margin: 0;
    font-size: 11.5px;
    font-weight: 600;
    line-height: 1.2;
    color: var(--jewel-ink);
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
    overflow: hidden;
}

.cast-picker-bar {
    display: flex;
    align-items: center;
    gap: 3px;
    flex-shrink: 0;
}

.cast-chip {
    appearance: none;
    border: 1.5px solid transparent;
    background: #faf0f5;
    border-radius: 50%;
    width: 28px;
    height: 28px;
    padding: 0;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    -webkit-tap-highlight-color: transparent;
    opacity: 0.58;
    transition: transform 0.15s ease, opacity 0.15s ease, border-color 0.15s ease;
}

.cast-chip img {
    width: 100%;
    height: 100%;
    border-radius: 50%;
    object-fit: cover;
    display: block;
}

.cast-chip.is-selected {
    opacity: 1;
    border-color: var(--jewel-rose);
    transform: scale(1.08);
    box-shadow: 0 1px 4px rgba(223, 105, 146, 0.28);
}

/* Scores and next/restart stay beside the companion, outside the playfield. */
.hud-row {
    grid-column: 3;
    min-width: 0;
    display: flex;
    flex-direction: column;
    align-items: stretch;
    justify-content: center;
    gap: 2px;
}

.hud-scores {
    display: flex;
    flex-direction: column;
    align-items: flex-start;
    gap: 0;
    min-width: 0;
}

.score-item {
    display: inline-flex;
    align-items: baseline;
    gap: 4px;
    line-height: 1.05;
}

.score-label {
    font-size: 9px;
    font-weight: 700;
    color: var(--jewel-muted);
}

.score-icon {
    font-size: 11px;
    color: var(--jewel-rose);
}

.score-item.is-best .score-icon {
    font-size: 11px;
}

.score-num {
    font-size: 16px;
    font-weight: 800;
    color: var(--jewel-ink);
    font-variant-numeric: tabular-nums;
    letter-spacing: -0.01em;
}

.score-item.is-best .score-num {
    font-size: 11.5px;
    color: var(--jewel-muted);
    font-weight: 700;
}

.hud-right {
    display: flex;
    align-items: center;
    gap: 5px;
    flex-shrink: 0;
}

.next-orb {
    display: inline-flex;
    align-items: center;
    gap: 4px;
    padding: 1px 5px;
    border-radius: 999px;
    background: #fff9fb;
    border: 1px solid var(--jewel-gold-light);
}

.next-tag {
    font-size: 10.5px;
    font-weight: 800;
    letter-spacing: 0.06em;
    color: var(--jewel-gold-deep);
}

#nextCanvas {
    width: 26px;
    height: 26px;
    display: block;
}

.icon-btn {
    appearance: none;
    border: 1px solid #ead4df;
    background: #fff;
    color: var(--jewel-muted);
    width: 32px;
    height: 32px;
    border-radius: 50%;
    padding: 0;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    -webkit-tap-highlight-color: transparent;
    transition: transform 0.12s ease, color 0.15s ease;
}

.icon-btn:active {
    transform: scale(0.92);
    color: var(--jewel-ink);
}

/* Jewelry Box Canvas Area */
.box-container {
    display: grid;
    grid-template-rows: minmax(0, 1fr) 36px;
    gap: 3px;
    min-width: 0;
    min-height: 0;
}

/* Size containment makes the board follow actual remaining space, including
   shared-header changes, instead of subtracting a guessed header height. */
.board-space {
    container-type: size;
    min-width: 0;
    min-height: 0;
    display: grid;
    place-items: center;
}

.jewelry-frame {
    position: relative;
    /* 5px frame on each side of the unchanged 340 x 436 physics canvas. */
    width: min(100cqw, calc((100cqh - 10px) * 340 / 436 + 10px));
    height: min(100cqh, calc((100cqw - 10px) * 436 / 340 + 10px));
    border-radius: 20px;
    padding: 5px;
    background: linear-gradient(145deg, #f8e7c6 0%, #d8ad72 50%, #f5e2bc 100%);
    box-shadow:
        0 10px 26px rgba(132, 76, 102, 0.14),
        inset 0 1px 2px rgba(255, 255, 255, 0.9);
    display: flex;
    align-items: center;
    justify-content: center;
}

#gameCanvas {
    width: 100%;
    height: 100%;
    display: block;
    border-radius: 15px;
    touch-action: none;
    cursor: pointer;
}

.chain-banner {
    position: absolute;
    top: 12px;
    left: 50%;
    transform: translateX(-50%);
    padding: 2px 10px;
    border-radius: 999px;
    background: rgba(223, 92, 138, 0.9);
    color: #fff;
    font-size: 11px;
    font-weight: 800;
    max-width: calc(100% - 16px);
    width: max-content;
    text-align: center;
    letter-spacing: 0.02em;
    pointer-events: none;
    animation: popBanner 0.24s cubic-bezier(0.2, 1.3, 0.4, 1);
}

/* Compact progression: next goal and the full evolution ladder. */
.evo-mini-bar {
    appearance: none;
    width: 100%;
    max-width: 100%;
    min-width: 0;
    height: 36px;
    border: 1px solid var(--jewel-border);
    background: var(--jewel-surface);
    border-radius: 999px;
    padding: 3px 10px;
    display: flex;
    align-items: center;
    cursor: pointer;
    -webkit-tap-highlight-color: transparent;
}

.evo-copy { flex: 0 0 82px; min-width: 0; text-align: left; line-height: 1.1; }
.journey-mini { display: block; margin-top: 2px; font-size: 8px; font-weight: 800; color: var(--jewel-rose); }
.evo-goal { display: block; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; text-align: left; font-size: 9px; font-weight: 800; color: var(--jewel-gold-deep); }

.evo-mini-icons {
    flex: 1;
    min-width: 0;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 1px;
    width: 100%;
}

.evo-dot {
    width: clamp(16px, 4.5vw, 20px);
    min-width: 0;
    height: 22px;
    border-radius: 50%;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    opacity: 0.32;
    transform: scale(0.88);
    transition: opacity 0.2s ease, transform 0.2s ease, box-shadow 0.2s ease;
}

.evo-dot.is-unlocked {
    opacity: 1;
    transform: scale(1);
}

.evo-dot.is-max {
    outline: 1.5px solid var(--jewel-gold);
    outline-offset: -1.5px;
    border-radius: 5px;
    background: #f7e7c5;
}

.evo-dot canvas {
    width: 100%;
    max-width: 20px;
    height: auto;
    display: block;
}

.evo-dot.is-target { opacity: 1; outline: 1.5px solid var(--jewel-rose); outline-offset: -1.5px; border-radius: 5px; background: #fff4f8; }
.collection-progress { margin: 0 0 10px; font-size: 11px; color: var(--jewel-muted); }
.chart-item.is-locked canvas { filter: grayscale(1); opacity: .4; }
.chart-item.is-target { border-color: var(--jewel-rose); background: #fff4f8; }
.chart-item-status { font-size: 8px; color: var(--jewel-muted); }

/* Dialogs */
.jewel-dialog {
    border: none;
    padding: 0;
    background: transparent;
    max-width: min(90vw, 340px);
    width: 100%;
    color: var(--jewel-ink);
}

.jewel-dialog::backdrop {
    background: rgba(48, 24, 36, 0.42);
    backdrop-filter: blur(3px);
}

.dialog-card {
    background: linear-gradient(180deg, #fffcfd 0%, #faf0f5 100%);
    border: 1.5px solid var(--jewel-gold-light);
    border-radius: 22px;
    padding: 16px;
    box-shadow: 0 16px 36px rgba(68, 28, 48, 0.22);
}

.dialog-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-bottom: 10px;
}

.dialog-header h2 {
    margin: 0;
    font-size: 14px;
    font-weight: 800;
    color: var(--jewel-ink);
}

.dialog-close {
    appearance: none;
    border: none;
    background: #f4e2ea;
    color: var(--jewel-ink);
    width: 26px;
    height: 26px;
    border-radius: 50%;
    font-size: 15px;
    font-weight: 700;
    cursor: pointer;
}

.chart-grid {
    list-style: none;
    margin: 0;
    padding: 0;
    display: grid;
    grid-template-columns: repeat(5, 1fr);
    gap: 8px 4px;
}

.chart-item {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 3px;
    background: #fff;
    border: 1px solid #f2e0e8;
    border-radius: 14px;
    padding: 7px 2px 5px;
}

.chart-item canvas {
    width: 36px;
    height: 36px;
}

.chart-item-name {
    font-size: 9.5px;
    font-weight: 700;
    color: var(--jewel-muted);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    max-width: 56px;
}

.dialog-card.is-confirm {
    text-align: center;
}

.dialog-card.is-confirm h2 {
    margin: 0 0 14px;
    font-size: 15px;
}

/* Result Dialog */
.dialog-card.is-result {
    text-align: center;
    padding: 18px 16px 16px;
}

.result-score-hero {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 12px;
    margin-bottom: 12px;
}

#resultMaxCanvas {
    width: 52px;
    height: 52px;
    flex-shrink: 0;
}

.result-numbers {
    display: flex;
    flex-direction: column;
    align-items: flex-start;
}

.result-main-score {
    font-size: 28px;
    font-weight: 800;
    line-height: 1.05;
    color: var(--jewel-ink);
    font-variant-numeric: tabular-nums;
}

.result-best-line {
    font-size: 12px;
    color: var(--jewel-muted);
    display: inline-flex;
    align-items: center;
    gap: 4px;
    margin-top: 2px;
}

.new-badge {
    font-size: 9px;
    font-weight: 800;
    padding: 1px 5px;
    border-radius: 999px;
    background: var(--jewel-rose);
    color: #fff;
}

.result-partner {
    display: flex;
    align-items: center;
    gap: 10px;
    text-align: left;
    background: #fff;
    border: 1px solid #f1dfe7;
    border-radius: 16px;
    padding: 9px 11px;
    margin-bottom: 12px;
}

.result-partner img {
    width: 78px;
    height: 82px;
    border-radius: 12px;
    object-fit: contain;
    border: 1.5px solid var(--jewel-gold-light);
    flex-shrink: 0;
}

.result-journey { margin: 0 0 12px; font-size: 12px; font-weight: 700; color: var(--jewel-gold-deep); }

.result-partner p {
    margin: 0;
    font-size: 12px;
    font-weight: 600;
    line-height: 1.38;
    color: var(--jewel-ink);
}

.dialog-actions {
    display: flex;
    gap: 8px;
}

.primary-jewel-btn,
.secondary-jewel-btn {
    flex: 1;
    appearance: none;
    border-radius: 999px;
    padding: 10px 12px;
    font-size: 13px;
    font-weight: 800;
    cursor: pointer;
}

.primary-jewel-btn {
    border: none;
    background: linear-gradient(135deg, #e56b96 0%, #ce945e 100%);
    color: #fff;
    box-shadow: 0 4px 10px rgba(216, 102, 144, 0.26);
}

.secondary-jewel-btn {
    border: 1px solid #e6ced9;
    background: #fff;
    color: var(--jewel-muted);
}

.noscript-msg {
    text-align: center;
    padding: 16px;
    font-size: 13px;
}

@keyframes popBanner {
    0% { transform: translateX(-50%) scale(0.7); opacity: 0; }
    100% { transform: translateX(-50%) scale(1); opacity: 1; }
}

@keyframes jewelCelebrate {
    0%, 100% { transform: scale(1); }
    50% { transform: translateY(-6px) rotate(12deg) scale(1.15); }
}

@keyframes avatarBounce {
    0% { transform: scale(1); }
    45% { transform: scale(1.1); }
    100% { transform: scale(1); }
}

@media (max-width: 380px) {
    .hud-scores { flex-direction: column; align-items: flex-start; gap: 0; }
    .score-item { line-height: 1.05; }
    .score-num { font-size: 14px; }
    .score-item.is-best .score-num, .score-item.is-best .score-icon { font-size: 10px; }
}

@media (prefers-reduced-motion: reduce) {
    *, *::before, *::after {
        animation-duration: 0.01ms !important;
        transition-duration: 0.01ms !important;
    }
}

.promise-heading { margin: 0 0 7px; font-size: 12px; color: var(--jewel-ink); }
.promise-heading small { margin-left: 5px; font-size: 9px; font-weight: 500; color: var(--jewel-muted); }
.promise-list { list-style: none; padding: 8px; margin: 0 0 14px; background: #fff9ed; border: 1px solid var(--jewel-gold-light); border-radius: 12px; }
.promise-item { display: grid; grid-template-columns: 16px 1fr 52px 38px; align-items: center; gap: 5px; min-height: 28px; font-size: 11px; font-weight: 700; }
.promise-star { color: var(--jewel-gold-deep); font-size: 16px; }
.promise-item progress { width: 52px; height: 5px; appearance: none; border: none; border-radius: 8px; overflow: hidden; background: #f0e2ce; accent-color: var(--jewel-rose); }
.promise-item progress::-webkit-progress-bar { background: #f0e2ce; }
.promise-item progress::-webkit-progress-value { background: var(--jewel-rose); }
.promise-item progress::-moz-progress-bar { background: var(--jewel-rose); }
.promise-value { font-size: 9px; text-align: right; color: var(--jewel-gold-deep); }
.result-promises { margin: -5px 0 12px; color: var(--jewel-rose); font-size: 12px; font-weight: 800; }

/* Keep the expanded cast selection within the screen. */
.cast-picker-bar {flex:1; min-width:0; max-width:150px; overflow-x:auto; padding:2px; scrollbar-width:none;}
.cast-picker-bar::-webkit-scrollbar {display:none;}
.cast-chip {flex-shrink:0;}
