/* CAACOTyping — Excel mini-game: 「公式帳冊」 (Formula Ledger).
   Self-contained; mirrors the styling conventions of
   styles/minigame-c4.css and styles/minigame-word-defense.css. */

.xp-ledger {
    position: fixed;
    inset: 0;
    z-index: 9500;                     /* sits under launchers, above taskbar */
    display: flex;
    align-items: center;
    justify-content: center;
    background: rgba(15, 30, 25, 0.45);
    backdrop-filter: blur(2px);
    -webkit-backdrop-filter: blur(2px);
    font-family: "Segoe UI", "Microsoft JhengHei", Tahoma, sans-serif;
    color: #1c2733;
}
.xp-ledger[hidden] { display: none !important; }

.xp-ledger__window {
    position: relative;
    width: min(680px, 94vw);
    max-height: 94vh;
    background: linear-gradient(180deg, #fdfdf6 0%, #f1eedf 100%);
    border: 1px solid #8a8770;
    border-radius: 6px;
    box-shadow:
        0 20px 50px rgba(0, 0, 0, 0.45),
        0 2px 6px rgba(0, 0, 0, 0.18);
    display: flex;
    flex-direction: column;
    overflow: hidden;
}

.xp-ledger__titlebar {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 8px 12px;
    background: linear-gradient(180deg, #1f6b3d 0%, #114b27 100%);
    color: #fff;
    user-select: none;
}
.xp-ledger__titlebar .xp-ledger__icon {
    width: 22px; height: 22px;
    background: #fff;
    border-radius: 3px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    color: #107c41;
    font-weight: 700;
    font-size: 14px;
    box-shadow: 0 1px 2px rgba(0, 0, 0, 0.25);
}
.xp-ledger__title {
    flex: 1;
    font-size: 14px;
    font-weight: 600;
    letter-spacing: 0.3px;
    text-shadow: 1px 1px 1px rgba(0, 0, 0, 0.35);
}
.xp-ledger__close {
    width: 26px; height: 22px;
    border: 1px solid rgba(0, 0, 0, 0.35);
    border-radius: 3px;
    background: linear-gradient(180deg, #e8e3d0 0%, #b8b09a 100%);
    color: #1c2733;
    font-weight: 700;
    cursor: pointer;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    line-height: 1;
}
.xp-ledger__close:hover { filter: brightness(1.05); }
.xp-ledger__close:active { transform: translateY(1px); }

.xp-ledger__body {
    flex: 1;
    display: flex;
    flex-direction: column;
    padding: 14px 18px 18px;
    gap: 14px;
    min-height: 360px;
}

/* --- Setup screen --- */
.xp-ledger__setup {
    display: flex;
    flex-direction: column;
    gap: 14px;
    align-items: flex-start;
}
.xp-ledger__setup h3 {
    margin: 4px 0 0;
    font-size: 18px;
    color: #114b27;
}
.xp-ledger__setup p {
    margin: 0;
    color: #4a4a3a;
    font-size: 13px;
    line-height: 1.6;
}
.xp-ledger__rules {
    background: #fff;
    border: 1px solid #d8d4be;
    border-left: 3px solid #107c41;
    border-radius: 4px;
    padding: 10px 14px;
    font-size: 12.5px;
    color: #3a3a2a;
    line-height: 1.7;
}
.xp-ledger__rules ul { margin: 6px 0 0; padding-left: 20px; }
.xp-ledger__start {
    align-self: stretch;
    font: inherit;
    font-size: 14px;
    padding: 10px 20px;
    border: 1px solid #114b27;
    border-radius: 4px;
    background: linear-gradient(180deg, #2a8a51 0%, #1f6b3d 100%);
    color: #fff;
    cursor: pointer;
    transition: filter 120ms ease;
}
.xp-ledger__start:hover { filter: brightness(1.08); }
.xp-ledger__start:active { transform: translateY(1px); }

/* --- Settings panel (mode + topics) --- */
.xp-ledger__settings {
    width: 100%;
    align-self: stretch;
    margin-top: 4px;
    border: 1px solid #d8d4be;
    border-radius: 4px;
    background: #f6f4e8;
    overflow: hidden;
}
.xp-ledger__settings-toggle {
    display: flex;
    align-items: center;
    gap: 8px;
    width: 100%;
    border: 0;
    background: transparent;
    font: inherit;
    font-size: 13px;
    font-weight: 600;
    color: #2a3a1f;
    padding: 8px 12px;
    text-align: left;
    cursor: pointer;
}
.xp-ledger__settings-toggle:hover { background: #ece9d4; }
.xp-ledger__settings-caret {
    display: inline-block;
    width: 12px;
    color: #114b27;
    font-size: 12px;
}
.xp-ledger__settings-summary {
    margin-left: auto;
    color: #6a6a55;
    font-size: 12px;
    font-weight: 400;
}
.xp-ledger__settings-panel {
    padding: 4px 12px 12px;
    border-top: 1px dashed #d0cba9;
    display: flex;
    flex-direction: column;
    gap: 12px;
}
.xp-ledger__settings-row {
    display: flex;
    flex-direction: column;
    gap: 8px;
}
.xp-ledger__settings-label {
    font-size: 12px;
    font-weight: 600;
    color: #3a3a2a;
    display: flex;
    align-items: center;
    gap: 8px;
}
.xp-ledger__settings-link {
    border: 0;
    background: transparent;
    color: #107c41;
    font: inherit;
    font-size: 11.5px;
    text-decoration: underline;
    cursor: pointer;
    padding: 0;
}
.xp-ledger__settings-link:hover { color: #0d6034; }
.xp-ledger__modes {
    display: grid;
    grid-template-columns: 1fr 1fr 1fr;
    gap: 8px;
}
.xp-ledger__mode {
    position: relative;
    display: block;
    cursor: pointer;
}
.xp-ledger__mode input[type="radio"] {
    position: absolute;
    opacity: 0;
    width: 0;
    height: 0;
}
.xp-ledger__mode-card {
    border: 1px solid #d8d4be;
    border-radius: 4px;
    background: #fff;
    padding: 8px 10px;
    transition: border-color 100ms ease, background 100ms ease;
    min-height: 56px;
}
.xp-ledger__mode-title {
    font-size: 13px;
    font-weight: 600;
    color: #2a3a1f;
}
.xp-ledger__mode-desc {
    margin-top: 2px;
    font-size: 11.5px;
    color: #6a6a55;
    line-height: 1.4;
}
.xp-ledger__mode input[type="radio"]:checked + .xp-ledger__mode-card {
    border-color: #107c41;
    background: #e7f3eb;
    box-shadow: inset 0 0 0 1px #107c41;
}
.xp-ledger__mode input[type="radio"]:focus-visible + .xp-ledger__mode-card {
    outline: 2px solid #107c41;
    outline-offset: 1px;
}

/* Question set selector (通用題庫 / 香港公文題庫 / …) — mirrors the
   difficulty card styling so the UI feels consistent. */
.xp-ledger__sets {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 6px 10px;
}
.xp-ledger__set {
    position: relative;
    display: block;
    cursor: pointer;
}
.xp-ledger__set input[type="radio"] {
    position: absolute;
    opacity: 0;
    width: 0;
    height: 0;
}
.xp-ledger__set-card {
    border: 1px solid #d8d4be;
    border-radius: 4px;
    background: #fff;
    padding: 8px 10px;
    transition: border-color 100ms ease, background 100ms ease;
    min-height: 56px;
}
.xp-ledger__set-title {
    font-size: 13px;
    font-weight: 600;
    color: #2a3a1f;
}
.xp-ledger__set-desc {
    margin-top: 2px;
    font-size: 11.5px;
    color: #6a6a55;
    line-height: 1.4;
}
.xp-ledger__set input[type="radio"]:checked + .xp-ledger__set-card {
    border-color: #107c41;
    background: #e7f3eb;
    box-shadow: inset 0 0 0 1px #107c41;
}
.xp-ledger__set input[type="radio"]:focus-visible + .xp-ledger__set-card {
    outline: 2px solid #107c41;
    outline-offset: 1px;
}

.xp-ledger__topics {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 6px 10px;
}
.xp-ledger__topic {
    position: relative;
    display: flex;
    align-items: flex-start;
    gap: 8px;
    padding: 6px 8px;
    border: 1px solid #e2dec4;
    border-radius: 3px;
    background: #fff;
    cursor: pointer;
    font-size: 12px;
    color: #3a3a2a;
    line-height: 1.35;
    transition: border-color 100ms ease, background 100ms ease;
}
.xp-ledger__topic:hover { border-color: #c0b98a; }
.xp-ledger__topic input[type="checkbox"] {
    margin: 2px 0 0;
    accent-color: #107c41;
}
.xp-ledger__topic-icon {
    width: 22px;
    height: 22px;
    flex: 0 0 22px;
    display: flex;
    align-items: center;
    justify-content: center;
    background: #f0eedb;
    border: 1px solid #d8d4be;
    border-radius: 3px;
    font-size: 12px;
    color: #114b27;
}
.xp-ledger__topic-body { display: flex; flex-direction: column; gap: 1px; }
.xp-ledger__topic-name { font-weight: 600; }
.xp-ledger__topic-hint { color: #6a6a55; font-size: 11px; }
.xp-ledger__topic input[type="checkbox"]:not(:checked) ~ * {
    color: #8a8870;
}
.xp-ledger__topic input[type="checkbox"]:not(:checked) ~ .xp-ledger__topic-icon {
    color: #a0a08a;
    background: #f6f4e8;
}
.xp-ledger__topic:has(input[type="checkbox"]:not(:checked)) {
    background: #f8f6e9;
}
.xp-ledger__topics-notice {
    grid-column: 1 / -1;
    font-size: 11.5px;
    color: #b14b2a;
    background: #fbeee6;
    border: 1px solid #f0d4c0;
    border-radius: 3px;
    padding: 4px 8px;
    opacity: 0;
    max-height: 0;
    overflow: hidden;
    transition: opacity 150ms ease, max-height 150ms ease, padding 150ms ease;
}
.xp-ledger__topics-notice.is-visible {
    opacity: 1;
    max-height: 60px;
    padding: 4px 8px;
}

/* --- Play screen --- */
.xp-ledger__hud {
    display: flex;
    align-items: center;
    gap: 18px;
    padding: 6px 0 2px;
    border-bottom: 1px dashed #d0cba9;
    font-size: 12.5px;
    color: #3a3a2a;
}
.xp-ledger__hud b { color: #114b27; font-weight: 700; }
.xp-ledger__hud .xl-ledger__hearts {
    font-size: 16px;
    color: #c0392b;
    letter-spacing: 1px;
}
.xp-ledger__hud .xl-ledger__hearts span { color: #d8d4be; }
.xp-ledger__hud .xl-ledger__hearts span.is-on { color: #c0392b; }

.xp-ledger__play.is-shaking {
    animation: xp-ledger-shake 220ms cubic-bezier(.36,.07,.19,.97) both;
}
@keyframes xp-ledger-shake {
    10%, 90% { transform: translateX(-2px); }
    20%, 80% { transform: translateX(3px); }
    30%, 50%, 70% { transform: translateX(-5px); }
    40%, 60% { transform: translateX(5px); }
}

.xp-ledger__page {
    position: relative;
    background: #fff;
    border: 1px solid #d8d4be;
    border-radius: 4px;
    padding: 14px 16px 12px;
    box-shadow: 0 1px 4px rgba(0, 0, 0, 0.06);
    transform-origin: 50% 50%;
    transition: transform 360ms ease, opacity 360ms ease;
}
.xp-ledger__page.is-entering {
    transform: translateY(18px);
    opacity: 0;
}

.xp-ledger__page.is-flipped {
    transform: rotateX(90deg) translateY(-40px);
    opacity: 0;
}
.xp-ledger__page.is-slammed {
    animation: xp-ledger-slam 360ms ease both;
}
@keyframes xp-ledger-slam {
    0%   { transform: translateX(0)   rotate(0); }
    18%  { transform: translateX(-7px) rotate(-1.5deg); }
    36%  { transform: translateX(6px)  rotate(1.5deg); }
    54%  { transform: translateX(-4px) rotate(-0.8deg); }
    72%  { transform: translateX(3px)  rotate(0.5deg); }
    100% { transform: translateX(0)   rotate(0); }
}

.xp-ledger__prompt {
    font-size: 14px;
    line-height: 1.6;
    color: #1c2733;
    margin-bottom: 10px;
}
.xp-ledger__target {
    display: inline-block;
    background: #fff5b1;
    border: 1px solid #d4a900;
    border-radius: 3px;
    padding: 0 6px;
    font-family: "Consolas", "Courier New", monospace;
    font-weight: 700;
    color: #6b4d00;
    margin: 0 1px;
}

.xp-ledger__grid { margin-bottom: 10px; }
.xp-ledger__empty-hint {
    color: #8a8770;
    font-style: italic;
    font-size: 12px;
    padding: 4px 0;
}
.xl-ledger__table {
    border-collapse: collapse;
    font-family: "Consolas", "Courier New", monospace;
    font-size: 12.5px;
    background: #fcfaf0;
    user-select: none;
}
.xl-ledger__corner,
.xl-ledger__colhead,
.xl-ledger__rowhead {
    background: #ebe6cf;
    color: #4a4a3a;
    border: 1px solid #c8c2a3;
    padding: 3px 6px;
    text-align: center;
    font-weight: 600;
    min-width: 28px;
}
.xl-ledger__cell {
    border: 1px solid #c8c2a3;
    padding: 4px 8px;
    min-width: 48px;
    text-align: right;
    color: #1c2733;
}
.xl-ledger__cell--target {
    background: #fff5b1;
    box-shadow: inset 0 0 0 2px #d4a900;
    color: #6b4d00;
    font-weight: 700;
}

.xp-ledger__hint {
    min-height: 18px;
    font-size: 12.5px;
    color: #4a4a3a;
}
.xp-ledger__hint.is-right { color: #1f6b3d; font-weight: 600; }
.xp-ledger__hint.is-wrong { color: #b03030; font-weight: 600; }

.xp-ledger__formula-bar {
    display: flex;
    align-items: center;
    gap: 6px;
    border: 1px solid #c8c2a3;
    border-radius: 3px;
    background: #fcfaf0;
    padding: 4px 8px;
    font-family: "Consolas", "Courier New", monospace;
}
.xp-ledger__formula-bar .xl-ledger__fx {
    color: #114b27;
    font-weight: 700;
    font-style: italic;
    font-size: 13px;
    padding-right: 6px;
    border-right: 1px solid #c8c2a3;
    user-select: none;
}
.xp-ledger__formula-bar input {
    flex: 1;
    font: inherit;
    font-size: 14px;
    border: 0;
    background: transparent;
    color: #1c2733;
    padding: 4px 4px;
    outline: none;
}
.xp-ledger__formula-bar input:focus {
    background: #fff;
    box-shadow: inset 0 0 0 1px #1f6b3d;
    border-radius: 2px;
}
.xp-ledger__submit {
    font: inherit;
    font-size: 13px;
    padding: 6px 16px;
    border: 1px solid #114b27;
    border-radius: 3px;
    background: linear-gradient(180deg, #2a8a51 0%, #1f6b3d 100%);
    color: #fff;
    cursor: pointer;
}
.xp-ledger__submit:hover { filter: brightness(1.08); }
.xp-ledger__submit:disabled {
    background: #b8b09a;
    border-color: #8a8770;
    cursor: not-allowed;
    filter: none;
}

/* --- End screen --- */
.xp-ledger__end {
    text-align: center;
    padding: 22px 0 6px;
}
.xp-ledger__end h3 {
    margin: 0 0 6px;
    color: #114b27;
    font-size: 18px;
}
.xp-ledger__end p {
    margin: 0 0 14px;
    color: #4a4a3a;
    font-size: 13px;
}
.xp-ledger__end-stats {
    display: flex;
    justify-content: center;
    gap: 30px;
    margin: 0 0 20px;
}
.xp-ledger__end-stats div {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 2px;
}
.xp-ledger__end-stats b {
    font-size: 28px;
    color: #1f6b3d;
    font-weight: 700;
    line-height: 1;
}
.xp-ledger__end-stats span {
    font-size: 11px;
    color: #6a6856;
    text-transform: uppercase;
    letter-spacing: 0.5px;
}
.xp-ledger__end-actions {
    display: flex;
    gap: 10px;
    justify-content: center;
    flex-wrap: wrap;
}
.xp-ledger__end-actions button {
    font: inherit;
    font-size: 13px;
    padding: 8px 18px;
    border: 1px solid #114b27;
    border-radius: 4px;
    background: #fff;
    color: #1c2733;
    cursor: pointer;
}
.xp-ledger__end-actions button.is-primary {
    background: linear-gradient(180deg, #2a8a51 0%, #1f6b3d 100%);
    color: #fff;
}
.xp-ledger__end-actions button:hover { filter: brightness(1.05); }

/* --- Mode banner (drill mode indicator) --- */
.xp-ledger__mode-banner {
    background: linear-gradient(180deg, #fff8d1 0%, #ffe89a 100%);
    border: 1px solid #d4a900;
    color: #6b4d00;
    padding: 6px 10px;
    border-radius: 4px;
    font-size: 12.5px;
    font-weight: 600;
    text-align: center;
    letter-spacing: 0.2px;
}

/* --- End-of-round recap --- */
.xp-ledger__end-recap {
    margin: 6px 0 14px;
    padding: 10px 12px;
    background: #fdfcf2;
    border: 1px solid #e0d9b8;
    border-radius: 4px;
    text-align: left;
    max-height: 220px;
    overflow-y: auto;
    font-size: 12px;
    color: #2a2a1a;
}
.xp-ledger__end-recap:empty { display: none; }
.xp-ledger__recap-empty {
    color: #6a6856;
    font-style: italic;
    padding: 4px 0;
    text-align: center;
}
.xp-ledger__recap-h {
    margin: 6px 0 4px;
    font-size: 11.5px;
    font-weight: 700;
    color: #114b27;
    text-transform: uppercase;
    letter-spacing: 0.6px;
}
.xp-ledger__recap-h:first-child { margin-top: 0; }

.xp-ledger__recap-list { display: flex; flex-direction: column; gap: 4px; }
.xp-ledger__recap-row {
    display: grid;
    grid-template-columns: 110px 1fr 78px;
    align-items: center;
    gap: 8px;
    padding: 3px 0;
}
.xp-ledger__recap-row.is-weak .xp-ledger__recap-num { color: #b03030; font-weight: 700; }
.xp-ledger__recap-row.is-weak .xp-ledger__recap-name { color: #b03030; }
.xp-ledger__recap-tag {
    display: flex;
    align-items: center;
    gap: 6px;
    font-size: 12px;
}
.xp-ledger__recap-icon {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 20px; height: 20px;
    background: #1f6b3d;
    color: #fff;
    border-radius: 3px;
    font-weight: 700;
    font-size: 12px;
}
.xp-ledger__recap-name { color: #2a2a1a; font-weight: 600; }
.xp-ledger__recap-bar {
    height: 8px;
    background: #ece7c7;
    border-radius: 4px;
    overflow: hidden;
    border: 1px solid #d8d4be;
}
.xp-ledger__recap-bar-fill {
    height: 100%;
    background: linear-gradient(90deg, #2a8a51 0%, #1f6b3d 100%);
    transition: width 240ms ease;
}
.xp-ledger__recap-row.is-weak .xp-ledger__recap-bar-fill {
    background: linear-gradient(90deg, #e08080 0%, #b03030 100%);
}
.xp-ledger__recap-num {
    text-align: right;
    font-variant-numeric: tabular-nums;
    color: #4a4a3a;
    font-size: 11.5px;
}

.xp-ledger__recap-wrong {
    margin: 4px 0 0;
    padding: 0 0 0 18px;
    list-style: decimal;
    color: #2a2a1a;
}
.xp-ledger__recap-wrong li { margin-bottom: 6px; }
.xp-ledger__recap-wrong-prompt { font-size: 12px; line-height: 1.5; }
.xp-ledger__recap-wrong-meta {
    color: #6a6856;
    font-size: 10.5px;
    margin-top: 1px;
    text-transform: uppercase;
    letter-spacing: 0.3px;
}
.xp-ledger__recap-wrong-answer {
    margin-top: 2px;
    font-family: "Consolas", "Courier New", monospace;
    font-size: 12px;
    color: #114b27;
    background: #eef7eb;
    border-left: 2px solid #1f6b3d;
    padding: 1px 6px;
}
.xp-ledger__recap-wrong-explain {
    color: #4a4a3a;
    font-size: 11.5px;
    margin-top: 2px;
    line-height: 1.5;
}

/* --- Compact / mobile --- */
@media (max-width: 540px) {
    .xp-ledger__window { width: 96vw; }
    .xp-ledger__body { padding: 10px 12px 14px; gap: 10px; }
    .xp-ledger__prompt { font-size: 13px; }
    .xl-ledger__cell { min-width: 36px; padding: 3px 5px; font-size: 11.5px; }
    .xp-ledger__end-stats { gap: 18px; }
    .xp-ledger__end-stats b { font-size: 22px; }
    .xp-ledger__recap-row {
        grid-template-columns: 80px 1fr 60px;
        gap: 4px;
    }
    .xp-ledger__recap-tag { font-size: 11px; }
    .xp-ledger__recap-num { font-size: 10.5px; }
}