/* ===== Browser app (app/) ===== */

/* Status, fields, checkboxes and link buttons are shared with the trainer
   page, which loads this stylesheet too. */

.app-status {
    display: flex;
    align-items: center;
    gap: var(--space-sm);
    margin-bottom: var(--space-lg);
    color: var(--text-secondary);
    font-size: 0.9rem;
}

.app-status__dot {
    flex: none;
    width: 8px;
    height: 8px;
    border-radius: 50%;
    background: var(--accent-warning);
}

.app-status[data-state="busy"] .app-status__dot {
    animation: app-pulse 1.2s ease-in-out infinite;
}

.app-status[data-state="ready"] .app-status__dot {
    background: var(--accent-success);
}

.app-status[data-state="error"] {
    color: var(--accent-warning);
}

@keyframes app-pulse {
    50% { opacity: 0.3; }
}

@media (prefers-reduced-motion: reduce) {
    .app-status[data-state="busy"] .app-status__dot { animation: none; }
}

.app-link-btn {
    background: none;
    border: none;
    padding: 0;
    color: var(--accent-primary);
    font: inherit;
    text-decoration: underline;
    cursor: pointer;
}

.app-link-btn:hover {
    color: var(--accent-hover);
}

.app-link-btn:disabled,
.drop-zone__browse:disabled {
    color: var(--text-dim);
    cursor: default;
}

.app-field {
    display: flex;
    flex-direction: column;
    gap: var(--space-xs);
    color: var(--text-secondary);
    font-size: 0.85rem;
}

.app-field input,
.app-field select {
    width: 100%;
    min-width: 0;
    padding: var(--space-sm) var(--space-md);
    background: var(--bg-surface);
    color: var(--text-primary);
    border: 1px solid var(--border);
    border-radius: 6px;
    font: inherit;
    font-size: 0.95rem;
}

.app-field input:focus,
.app-field select:focus {
    outline: none;
    border-color: var(--accent-primary);
    box-shadow: 0 0 0 3px rgba(91, 157, 217, 0.15);
}

.app-check {
    display: flex;
    align-items: center;
    gap: var(--space-sm);
    color: var(--text-primary);
    font-size: 0.95rem;
    cursor: pointer;
}

.app-check input {
    width: 16px;
    height: 16px;
    accent-color: var(--accent-primary);
}

.app-table {
    width: 100%;
    border-collapse: collapse;
    font-size: 0.9rem;
}

.app-table td {
    padding: var(--space-sm);
    border-bottom: 1px solid var(--border);
    white-space: nowrap;
    width: 1%;
}

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

.app-table tbody tr[data-index] {
    cursor: pointer;
}

.app-table tbody tr[data-index]:hover td {
    background: var(--bg-surface);
}

.app-table tbody tr.is-active td {
    background: rgba(91, 157, 217, 0.12);
}

.app-table tbody tr:focus-visible {
    outline: 2px solid var(--accent-primary);
    outline-offset: -2px;
}

.app-table__pick {
    padding-left: var(--space-md) !important;
}

.app-table__pick input {
    accent-color: var(--accent-primary);
}

.app-table__game,
.app-table__err {
    color: var(--text-secondary);
}

.app-table__move {
    width: auto !important;
    font-family: var(--font-code);
    font-size: 0.82rem;
    white-space: normal !important;
}

.app-table__err {
    text-align: right;
    font-variant-numeric: tabular-nums;
    padding-right: var(--space-md) !important;
}

.app-table__empty {
    padding: var(--space-xl) var(--space-md) !important;
    color: var(--text-dim);
    text-align: center;
    white-space: normal !important;
}

.app-tag {
    display: inline-block;
    padding: 1px 6px;
    border-radius: 4px;
    background: var(--bg-surface);
    color: var(--text-secondary);
    font-size: 0.75rem;
}

.app-tag + .app-tag {
    margin-left: var(--space-xs);
}

.app-tag--cube {
    color: var(--accent-warning);
}

.app-tag--note {
    color: var(--accent-success);
}

.app-seg {
    display: inline-flex;
    border: 1px solid var(--border);
    border-radius: 6px;
    overflow: hidden;
}

.app-seg__btn {
    padding: 6px var(--space-md);
    background: transparent;
    color: var(--text-secondary);
    border: none;
    font: inherit;
    font-size: 0.9rem;
    cursor: pointer;
}

.app-seg__btn + .app-seg__btn {
    border-left: 1px solid var(--border);
}

.app-seg__btn--on {
    background: var(--bg-surface);
    color: var(--text-primary);
}

.app-seg__btn:disabled {
    color: var(--text-dim);
    cursor: default;
}

.app-compare {
    width: 100%;
    border-collapse: collapse;
    margin: var(--space-md) 0;
    font-size: 0.95rem;
}

.app-compare th,
.app-compare td {
    padding: var(--space-sm) var(--space-md);
    border-bottom: 1px solid var(--border);
    text-align: left;
    vertical-align: top;
}

.app-compare thead th {
    color: var(--text-secondary);
    font-weight: 600;
}

.app-compare tbody th {
    color: var(--text-primary);
    font-weight: 500;
}

.app-compare td {
    color: var(--text-secondary);
}

/* ===== App shell: fills the window below the sticky site nav ===== */

.app-shell {
    display: grid;
    /* Wider screens give the list room for full names; the card stops at 800px anyway. */
    grid-template-columns: clamp(320px, 24vw, 440px) minmax(0, 1fr);
    grid-template-rows: auto minmax(0, 1fr);
    /* --app-nav is the nav's measured height, set by app.js; it wraps on phones. */
    height: calc(100vh - var(--app-nav, 73px));
    height: calc(100dvh - var(--app-nav, 73px));
    min-height: 540px;
    border-bottom: 1px solid var(--border);
}

.app-bar {
    grid-column: 1 / -1;
    display: flex;
    align-items: center;
    gap: var(--space-md);
    padding: 10px var(--space-lg);
    border-bottom: 1px solid var(--border);
    background: var(--bg-secondary);
}

.app-bar__group {
    display: flex;
    align-items: center;
    gap: var(--space-sm);
}

.app-bar__actions {
    margin-left: auto;
}

/* <button> doesn't inherit the page font; the site's other buttons are links. */
.app-shell button.btn,
.app-dialog button.btn {
    font-family: inherit;
}

.app-bar .btn {
    min-height: 36px;
    padding: 0 14px;
    white-space: nowrap;
}

.app-bar .btn:disabled,
.app-empty .btn:disabled {
    opacity: 0.45;
    cursor: default;
    transform: none;
    box-shadow: none;
}

/* Until a file is open, the stage gets the whole width. */
.app-shell[data-view="empty"] {
    grid-template-columns: minmax(0, 1fr);
}

.app-shell[data-view="empty"] .app-side {
    display: none;
}

.app-bar__status {
    flex: 1 1 auto;
    min-width: 0;
    margin: 0;
}

.app-bar__status #app-status-text {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.app-deck-chip,
.app-icon-btn {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    min-height: 36px;
    padding: 0 10px;
    border: 1px solid var(--border);
    border-radius: 6px;
    background: transparent;
    color: var(--text-primary);
    font: inherit;
    font-size: 0.85rem;
    cursor: pointer;
}

.app-deck-chip {
    max-width: 220px;
}

.app-deck-chip:hover,
.app-icon-btn:hover {
    border-color: var(--accent-primary);
}

.app-deck-chip__label {
    color: var(--text-dim);
}

.app-deck-chip__name {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.app-deck-chip__icon,
.app-icon-btn svg {
    flex: none;
    width: 16px;
    height: 16px;
    fill: none;
    stroke: currentColor;
    stroke-width: 2;
    stroke-linecap: round;
    stroke-linejoin: round;
}

.app-deck-chip__icon {
    color: var(--text-dim);
}

/* Sidebar: filter and the position list */
.app-side {
    display: flex;
    flex-direction: column;
    min-height: 0;
    border-right: 1px solid var(--border);
}

.app-side__meta {
    margin: 0;
    padding: var(--space-md) var(--space-lg) 0;
    color: var(--text-secondary);
    font-size: 0.85rem;
    line-height: 1.5;
}

.app-filter {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: var(--space-sm) var(--space-md);
    margin: var(--space-md) var(--space-lg) 0;
    padding: var(--space-md);
    border: 1px solid var(--border);
    border-radius: 8px;
    background: var(--bg-elevated);
}

.app-filter__title {
    grid-column: 1 / -1;
    margin: 0;
    color: var(--text-secondary);
    font-size: 0.8rem;
}

.app-list {
    display: flex;
    flex: 1;
    flex-direction: column;
    min-height: 0;
    margin-top: var(--space-md);
    border-top: 1px solid var(--border);
}

.app-list__head {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: var(--space-sm) var(--space-lg);
    border-bottom: 1px solid var(--border);
}

.app-list__count {
    color: var(--text-secondary);
    font-size: 0.85rem;
}

.app-list__scroll {
    flex: 1;
    min-height: 0;
    overflow-y: auto;
    /* Room for the floating Ko-fi button over the list's last rows. */
    padding-bottom: 72px;
}

/* max-width: 0 lets the text cell take the leftover width and still truncate. */
.app-row {
    width: 100% !important;
    max-width: 0;
    white-space: normal !important;
    padding-right: var(--space-lg) !important;
}

.app-row__top,
.app-row__bottom {
    display: flex;
    align-items: baseline;
    gap: var(--space-sm);
    min-width: 0;
}

.app-row__bottom {
    margin-top: 2px;
}

.app-row__who {
    flex: 1;
    min-width: 0;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    color: var(--text-primary);
}

.app-row__err {
    color: var(--text-secondary);
    font-variant-numeric: tabular-nums;
}

.app-row__move {
    flex: 1;
    min-width: 0;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    font-family: var(--font-code);
    font-size: 0.8rem;
    color: var(--text-secondary);
}

.app-row__game {
    color: var(--text-dim);
    font-size: 0.75rem;
    white-space: nowrap;
}

/* Stage: empty state, or the card as Anki shows it */
.app-stage {
    position: relative;
    display: flex;
    flex-direction: column;
    min-width: 0;
    min-height: 0;
    background: var(--bg-secondary);
}

.app-banner {
    display: flex;
    align-items: flex-start;
    gap: var(--space-md);
    margin: var(--space-md) var(--space-lg) 0;
    padding: var(--space-sm) var(--space-md);
    border-left: 4px solid var(--accent-warning);
    border-radius: 6px;
    background: var(--bg-elevated);
}

.app-banner__message {
    flex: 1;
    margin: 0;
    color: var(--accent-warning);
    font-size: 0.9rem;
    line-height: 1.5;
}

.app-banner__close {
    border: 0;
    background: none;
    color: var(--text-secondary);
    font-size: 1.3rem;
    line-height: 1;
    cursor: pointer;
}

.app-empty {
    max-width: 560px;
    margin: auto;
    padding: var(--space-2xl) var(--space-lg);
    text-align: center;
}

.app-empty__dice {
    width: 112px;
    height: auto;
    color: var(--text-dim);
}

.app-empty__dice rect {
    fill: var(--bg-surface);
    stroke: currentColor;
    stroke-width: 3;
}

.app-empty__dice circle {
    fill: currentColor;
}

.app-empty__title {
    margin: var(--space-lg) 0 var(--space-md);
    font-size: 1.9rem;
    line-height: 1.2;
    text-wrap: balance;
}

.app-empty__lead {
    color: var(--text-secondary);
    line-height: 1.6;
}

.app-empty__actions {
    display: flex;
    flex-wrap: wrap;
    justify-content: center;
    gap: var(--space-sm);
    margin: var(--space-xl) 0 var(--space-md);
}

/* Startup progress, shown where the Open and Paste actions appear once ready */
.app-boot {
    max-width: 420px;
    margin: var(--space-xl) auto var(--space-md);
    padding: var(--space-lg);
    border: 1px solid var(--border);
    border-radius: 12px;
    background: var(--bg-elevated);
    text-align: left;
}

.app-boot__steps {
    display: grid;
    gap: var(--space-md);
    margin: 0;
    padding: 0;
    list-style: none;
}

.app-boot__step {
    display: flex;
    align-items: center;
    gap: var(--space-md);
    color: var(--text-dim);
    font-size: 1rem;
}

.app-boot__step[data-state="active"],
.app-boot__step[data-state="done"] {
    color: var(--text-primary);
}

.app-boot__step[data-state="error"] {
    color: var(--accent-warning);
}

.app-boot__icon {
    position: relative;
    flex: none;
    width: 22px;
    height: 22px;
    border: 2px solid var(--border);
    border-radius: 50%;
}

.app-boot__step[data-state="active"] .app-boot__icon {
    border-color: var(--border);
    border-top-color: var(--accent-primary);
    animation: app-spin 0.8s linear infinite;
}

.app-boot__step[data-state="done"] .app-boot__icon {
    border-color: var(--accent-success);
    background: var(--accent-success);
}

.app-boot__step[data-state="done"] .app-boot__icon::after {
    content: "";
    position: absolute;
    left: 6px;
    top: 2px;
    width: 5px;
    height: 10px;
    border: solid var(--bg-primary);
    border-width: 0 2px 2px 0;
    transform: rotate(45deg);
}

.app-boot__step[data-state="error"] .app-boot__icon {
    border-color: var(--accent-warning);
}

.app-boot__step[data-state="error"] .app-boot__icon::after {
    content: "!";
    position: absolute;
    inset: 0;
    display: grid;
    place-items: center;
    color: var(--accent-warning);
    font-size: 0.8rem;
    font-weight: 700;
}

.app-boot__note {
    margin: var(--space-md) 0 0;
    color: var(--text-secondary);
    font-size: 0.85rem;
    line-height: 1.5;
}

.app-boot[data-state="error"] .app-boot__note {
    color: var(--accent-warning);
}

.app-boot__failed {
    display: flex;
    flex-wrap: wrap;
    gap: var(--space-sm);
    margin-top: var(--space-md);
}

@keyframes app-spin {
    to { transform: rotate(360deg); }
}

@media (prefers-reduced-motion: reduce) {
    .app-boot__step[data-state="active"] .app-boot__icon {
        animation: none;
        border-color: var(--accent-primary);
    }
}

.app-empty__sample {
    color: var(--text-secondary);
}

.app-empty__limits {
    margin-top: var(--space-xl);
    padding-top: var(--space-md);
    border-top: 1px solid var(--border);
    color: var(--text-secondary);
    font-size: 0.85rem;
    line-height: 1.6;
}

/* Links in running text need more than color to stand out (WCAG 1.4.1). */
.app-empty__limits a {
    color: var(--accent-primary);
    text-decoration: underline;
    text-underline-offset: 2px;
}

.app-preview {
    display: flex;
    flex: 1;
    flex-direction: column;
    gap: var(--space-sm);
    min-height: 0;
    padding: var(--space-md) var(--space-lg) var(--space-lg);
}

.app-preview__bar {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: var(--space-sm) var(--space-md);
}

.app-preview__hint {
    color: var(--text-dim);
    font-size: 0.85rem;
}

.app-preview__frame {
    flex: 1;
    width: 100%;
    min-height: 0;
    border: 0;
    border-radius: 10px;
    box-shadow: 0 0 0 1px var(--border);
}

.app-drop {
    position: absolute;
    inset: var(--space-md);
    z-index: 5;
    display: flex;
    align-items: center;
    justify-content: center;
    border: 2px dashed var(--accent-primary);
    border-radius: 12px;
    background: rgba(18, 18, 18, 0.85);
    color: var(--text-primary);
    font-size: 1.1rem;
    pointer-events: none;
}

/* Dialogs: card options, paste, Send to Anki help */
/* margin: auto restores the browser's dialog centering, which reset.css zeroes. */
.app-dialog {
    margin: auto;
    width: min(640px, calc(100vw - 32px));
    max-height: calc(100dvh - 48px);
    padding: 0;
    border: 1px solid var(--border);
    border-radius: 12px;
    background: var(--bg-elevated);
    color: var(--text-primary);
}

.app-dialog::backdrop {
    background: rgba(0, 0, 0, 0.6);
}

.app-dialog__body {
    display: flex;
    flex-direction: column;
    gap: var(--space-md);
    padding: var(--space-lg);
}

.app-dialog__title {
    font-size: 1.25rem;
}

.app-dialog__section {
    display: flex;
    flex-direction: column;
    gap: var(--space-sm);
    padding-top: var(--space-md);
    border-top: 1px solid var(--border);
}

.app-dialog__grid {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: var(--space-sm) var(--space-lg);
}

.app-dialog__heading {
    grid-column: 1 / -1;
    color: var(--text-secondary);
    font-size: 0.8rem;
    font-weight: 600;
    letter-spacing: 0.06em;
    text-transform: uppercase;
}

.app-dialog__note {
    color: var(--text-secondary);
    font-size: 0.85rem;
    line-height: 1.5;
}

.app-dialog__textarea {
    min-height: 220px;
}

.app-dialog__footer {
    display: flex;
    justify-content: flex-end;
    gap: var(--space-sm);
    padding-top: var(--space-sm);
}

.app-anki-help {
    margin: 0;
    padding-left: 1.2em;
    color: var(--text-secondary);
    font-size: 0.9rem;
    line-height: 1.6;
}

.app-anki-help li + li {
    margin-top: var(--space-xs);
}

.app-anki-help code {
    color: var(--text-primary);
    font-family: var(--font-code);
    font-size: 0.8rem;
    overflow-wrap: anywhere;
}

/* Tablets and phones: one column that scrolls with the page */
@media (max-width: 900px) {
    /* Until a file is open, the empty state already offers Open and Paste,
       and there is nothing to export yet. */
    .app-shell[data-view="empty"] .app-bar__group {
        display: none;
    }

    .app-shell {
        display: flex;
        flex-direction: column;
        height: auto;
        min-height: 0;
    }

    .app-bar {
        flex-wrap: wrap;
        padding: var(--space-sm) var(--space-md);
    }

    .app-bar__status {
        order: 3;
        flex-basis: 100%;
    }

    .app-bar__status #app-status-text {
        white-space: normal;
    }

    .app-bar__actions {
        flex-wrap: wrap;
        width: 100%;
        margin-left: 0;
    }

    .app-bar__actions .btn {
        flex: 1 1 auto;
    }

    .app-side {
        border-right: 0;
        border-bottom: 1px solid var(--border);
    }

    .app-list__scroll {
        max-height: 40vh;
        padding-bottom: 0;
    }

    .app-preview {
        padding: var(--space-md);
        scroll-margin-top: var(--app-nav, 0px);
    }

    /* Taller than wide, so the card's phone-landscape rules (landscape and
       under 600px high) stay off, as they do on a phone held upright. */
    .app-preview__frame {
        flex: none;
        height: auto;
        aspect-ratio: 5 / 8;
    }

    .app-empty {
        padding: var(--space-xl) var(--space-md);
    }

    .app-empty__title {
        font-size: 1.5rem;
    }
}

@media (max-width: 640px) {
    .app-dialog__grid {
        grid-template-columns: minmax(0, 1fr);
    }

    .app-deck-chip {
        max-width: 160px;
    }
}
