/* ============================================================
   kanji.css — Kanji mode styles.

   All UI scoped under body.kanji-active. Mirrors the
   vocab.css / grammar.css conventions (trigger highlight,
   left-column list swap, mobile height clamp). Pairs with
   js/kanji-study.js.

   First-slice scope: toolbar button + left-column kanji list
   with All/Lessons toggle + right-column details skeleton.
   Center column (writing canvas wiring) lands in a follow-up.
   ============================================================ */

/* Kanji toggle — match the accent highlight pattern used by
   Vocabulary / Grammar so the user has a single, unambiguous
   "you are here" indicator in the toolbar. */
.kanji-trigger.active {
    background: transparent;
    color: var(--accent);
    border: 1.5px solid var(--accent);
    font-weight: 700;
}
@media (hover: hover) {
    .kanji-trigger.active:hover {
        background: var(--accent-bg);
        color: var(--accent);
        border-color: var(--accent);
    }
}

/* While kanji mode is active, demote the canvas-side Trace/Recall
   highlight — the user isn't in either kana mode right now. */
body.kanji-active .mode-toggle-btn.active {
    background: transparent;
    color: var(--text-muted);
    border: var(--dash-soft);
    font-weight: 600;
}

/* Default state: kanji list hidden so the existing grid +
   subheaders show exactly as they did before this feature. */
.kanji-list-wrap { display: none; }

body.kanji-active #char-grid,
body.kanji-active .grid-subcol-headers {
    display: none;
}
body.kanji-active .kanji-list-wrap {
    display: flex;
    flex-direction: column;
    flex: 1 1 0;
    min-height: 0;
}

/* In kanji mode the list owns scroll so the column header
   stays put while kanji items scroll underneath. */
body.kanji-active .col-left .panel-body {
    overflow: hidden;
    padding: 0;
    display: flex;
    flex-direction: column;
}

body.kanji-active .kanji-list {
    flex: 1 1 0;
    min-height: 0;
    overflow-y: auto;
    padding: 2px 0 6px;
}

@media (max-width: 900px) {
    /* On mobile the column itself has no fixed height (col-left is
       just width:100% in the tab layout), so flex:1 1 0 on the wrap
       collapses to 0 — which is why the All/Lessons toggle and the
       kanji tiles read as an empty column. Give both the wrap and
       the list explicit vertical room so the tiles render. */
    body.kanji-active .kanji-list-wrap {
        flex: none;
        height: 65vh;
    }
    body.kanji-active .kanji-list {
        flex: 1 1 auto;
        min-height: 0;
        height: auto;
    }
}

/* ── All / Lessons view toggle ─────────────────────────── */
.kanji-view-toggle {
    display: flex;
    margin: 6px 8px 8px;
    border: 1.5px solid var(--subtle-border);
    border-radius: 8px;
    overflow: hidden;
    background: var(--bg);
    flex-shrink: 0;
}

.kanji-view-btn {
    flex: 1 1 0;
    border: 0;
    background: transparent;
    color: var(--text-muted);
    /* 3 tabs with longer labels (Grammar Lessons / Custom Lessons /
       All) — drop the font + line-height so they fit without wrap. */
    font-size: 0.72rem;
    font-weight: 600;
    line-height: 1.15;
    padding: 6px 4px;
    cursor: pointer;
    transition: background 0.12s, color 0.12s;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.kanji-view-btn.active {
    background: var(--accent);
    color: #fff;
}

@media (hover: hover) {
    .kanji-view-btn:not(.active):hover {
        background: var(--accent-bg);
        color: var(--accent);
    }
}

/* ── All-view search bar ──────────────────────────────── */
/* Sits between the Lessons/All toggle and the kanji list. Hidden by
   default; JS unhides when the user switches to All view. */
.kanji-search-wrap {
    padding: 6px 4px 8px;
}
.kanji-search-wrap[hidden] { display: none; }

.kanji-search-input {
    width: 100%;
    box-sizing: border-box;
    padding: 7px 12px;
    border: 1px solid var(--subtle-border);
    border-radius: 8px;
    background: var(--bg-card, transparent);
    color: var(--text);
    font-size: 0.9rem;
    font-family: inherit;
    outline: none;
    transition: border-color 0.15s;
}
.kanji-search-input:focus {
    border-color: var(--accent);
}
.kanji-search-input::placeholder {
    color: var(--text-faint);
}

.kanji-search-empty,
.kanji-custom-empty {
    padding: 18px 12px;
    font-size: 0.85rem;
    color: var(--text-muted);
    text-align: center;
}

/* ── Section header (lesson name or JLPT tier) ────────── */
.kanji-section-header {
    font-size: 0.72rem;
    text-transform: uppercase;
    letter-spacing: 0.08em;
    color: var(--text-faint);
    padding: 12px 12px 6px;
    font-weight: 700;
}
.kanji-section-header:first-child {
    padding-top: 6px;
}

/* Collapsible toggle variant (All view). The header is a real <button>
   so keyboard/AT use is free; layout matches the static lesson-view
   header but adds a caret on the left and a count chip on the right. */
.kanji-section-toggle {
    display: flex;
    align-items: center;
    gap: 8px;
    width: 100%;
    background: transparent;
    border: none;
    cursor: pointer;
    font-family: inherit;
    text-align: left;
    /* Reuse the static header's color/case rules above. */
}
.kanji-section-toggle .kanji-section-name { flex: 1; }
.kanji-section-toggle .kanji-section-caret {
    display: inline-block;
    transition: transform 0.15s;
    font-size: 0.7rem;
    line-height: 1;
}
.kanji-section-toggle.collapsed .kanji-section-caret {
    transform: rotate(-90deg);
}
.kanji-section-toggle .kanji-section-count {
    font-variant-numeric: tabular-nums;
    color: var(--text-muted);
    font-weight: 600;
    font-size: 0.7rem;
}
@media (hover: hover) {
    .kanji-section-toggle:hover { color: var(--text); }
    .kanji-section-toggle:hover .kanji-section-count { color: var(--text); }
}

/* ── Kanji tile grid within each section ──────────────── */
.kanji-section {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(54px, 1fr));
    gap: 6px;
    padding: 4px 8px 12px;
}

/* Wrapper around each tile so the "+" add-to-lesson button can live
   as a sibling instead of a nested button (nested <button>s are
   invalid HTML). Wrapper carries the aspect-ratio so the grid keeps
   its square cells; the tile inside fills 100% × 100%. */
.kanji-tile-wrap {
    position: relative;
    aspect-ratio: 1 / 1;
    /* Show the "+" only on hover for desktop; on touch we always show
       it (no hover affordance available). See @media below. */
}

.kanji-tile-add {
    position: absolute;
    top: 2px;
    right: 2px;
    width: 18px;
    height: 18px;
    padding: 0;
    border: 1px solid var(--subtle-border);
    border-radius: 50%;
    background: var(--bg);
    color: var(--text-faint);
    font-size: 0.85rem;
    font-weight: 700;
    line-height: 1;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    opacity: 0;
    transition: opacity 0.12s, background 0.12s, color 0.12s, border-color 0.12s;
    z-index: 1;
}
@media (hover: hover) {
    .kanji-tile-wrap:hover .kanji-tile-add,
    .kanji-tile-add:focus-visible {
        opacity: 1;
    }
    .kanji-tile-add:hover {
        background: var(--accent);
        color: #fff;
        border-color: var(--accent);
    }
}
@media (hover: none) {
    /* Touch devices have no hover state — keep the add affordance
       visible but understated. */
    .kanji-tile-add { opacity: 0.5; }
}

.kanji-tile {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    width: 100%;
    height: 100%;
    background: var(--bg);
    border: 1px solid var(--subtle-border);
    border-radius: 6px;
    font-family: 'Noto Sans JP', sans-serif;
    cursor: pointer;
    transition: background 0.12s, border-color 0.12s, transform 0.08s;
    line-height: 1;
}

.kanji-tile-glyph {
    font-family: 'Klee One', 'Noto Sans JP', sans-serif;
    font-size: 1.55rem;
    font-weight: 600;
    color: var(--text);
}

.kanji-tile-meaning {
    font-size: 0.6rem;
    color: var(--text-faint);
    margin-top: 4px;
    max-width: 100%;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    padding: 0 2px;
}

.kanji-tile.selected {
    background: var(--accent-bg);
    border-color: var(--accent);
}
.kanji-tile.selected .kanji-tile-glyph { color: var(--accent); }

@media (hover: hover) {
    .kanji-tile:hover {
        background: var(--accent-bg);
        border-color: var(--subtle-border);
    }
}

.kanji-tile:active { transform: translateY(1px); }

/* Visual marker for kanji that don't yet have stroke data
   ingested — still listable, but the Trace canvas can't drive
   them so they're dimmed and tagged. */
.kanji-tile.no-strokes {
    opacity: 0.55;
}
.kanji-tile.no-strokes::after {
    content: '○';
    position: absolute;
}

.kanji-list-error {
    padding: 16px;
    color: var(--text-faint);
    font-size: 0.85rem;
    font-style: italic;
    text-align: center;
}

/* ── Right column swap (Character Details ↔ Kanji Details) ── */
.kanji-details-panel { display: none; }

body.kanji-active #char-details,
body.kanji-active #vocab-details,
body.kanji-active #grammar-details {
    display: none;
}
body.kanji-active .kanji-details-panel {
    display: block;
}

body.kanji-active .col-right .panel-box {
    flex: 1 1 0;
    min-height: 0;
    max-height: none;
}
body.kanji-active .col-right .panel-body {
    min-height: 0;
}
@media (max-width: 900px) {
    body.kanji-active .col-right .panel-box {
        flex: none;
        height: 60vh;
    }
}

.kanji-details-panel {
    padding: 4px 4px 16px;
}

.kanji-details-empty {
    color: var(--text-faint);
    font-style: italic;
    padding: 16px;
    text-align: center;
}

/* ── Rich details panel ─────────────────────────────── */
.kanji-details-header {
    text-align: center;
    padding: 8px 4px 4px;
}

.kanji-details-glyph {
    font-family: 'Klee One', 'Noto Sans JP', sans-serif;
    font-size: 4rem;
    font-weight: 600;
    color: var(--text);
    line-height: 1.1;
}

.kanji-details-meaning {
    font-size: 1rem;
    color: var(--text);
    margin-top: 4px;
}

.kanji-details-meta {
    text-align: center;
    font-size: 0.78rem;
    color: var(--text-faint);
    padding: 0 4px 6px;
    border-bottom: 1px solid var(--subtle-border);
    margin-bottom: 6px;
}

.kanji-details-section-heading {
    font-size: 0.72rem;
    text-transform: uppercase;
    letter-spacing: 0.08em;
    color: var(--text-faint);
    font-weight: 700;
    margin: 12px 4px 6px;
}

/* Readings */
.kanji-readings { padding: 0 4px; }

.kanji-reading-row {
    display: flex;
    align-items: center;
    gap: 6px;
    flex-wrap: wrap;
    margin-bottom: 4px;
}

.kanji-reading-label {
    font-size: 0.7rem;
    font-weight: 700;
    color: var(--text-faint);
    text-transform: uppercase;
    letter-spacing: 0.06em;
    min-width: 28px;
}

.kanji-reading-row.onyomi .kanji-reading-label { color: var(--accent); }
.kanji-reading-row.kunyomi .kanji-reading-label { color: var(--text-muted); }

.kanji-reading-chip {
    font-family: 'Noto Sans JP', sans-serif;
    font-size: 0.95rem;
    background: var(--accent-bg);
    border: 1px solid var(--subtle-border);
    border-radius: 6px;
    padding: 2px 8px;
    color: var(--text);
}

.kanji-reading-row.kunyomi .kanji-reading-chip {
    background: var(--bg);
}

/* Compounds table */
.kanji-compounds-table {
    width: 100%;
    border-collapse: collapse;
    font-size: 0.9rem;
    margin: 4px 0;
}

.kanji-compounds-table td {
    padding: 5px 6px;
    border-bottom: 1px solid var(--subtle-border);
    vertical-align: top;
}

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

.kanji-compound-word {
    font-family: 'Noto Sans JP', sans-serif;
    font-weight: 600;
    color: var(--text);
    white-space: nowrap;
}

.kanji-compound-reading {
    font-family: 'Noto Sans JP', sans-serif;
    color: var(--text-muted);
    font-size: 0.82rem;
    white-space: nowrap;
}

.kanji-compound-meaning {
    color: var(--text-faint);
    font-size: 0.85rem;
}

/* Example sentences */
.kanji-sentence {
    padding: 6px 8px;
    margin-bottom: 6px;
    background: var(--accent-bg);
    border-left: 3px solid var(--accent);
    border-radius: 0 6px 6px 0;
}

.kanji-sentence-jp {
    font-family: 'Noto Sans JP', sans-serif;
    font-size: 0.95rem;
    color: var(--text);
}

.kanji-sentence-reading {
    font-family: 'Noto Sans JP', sans-serif;
    font-size: 0.78rem;
    color: var(--text-muted);
    margin-top: 2px;
}

.kanji-sentence-translation {
    font-size: 0.85rem;
    color: var(--text-faint);
    margin-top: 2px;
    font-style: italic;
}

/* Hide the per-column Report Card / Printable Practice Sheet
   action row — neither makes sense for kanji mode. Same pattern
   as Vocab and Grammar modes. */
body.kanji-active .col-right-actions {
    display: none;
}

/* ── Center column UI hide rules ───────────────────────
   Kanji mode keeps the writing canvas + the surrounding
   tagline, navigation arrows, and char-row (repurposed for
   sublesson kanji tiles). The kana-specific romaji /
   pronunciation widgets stay hidden because they reference
   kana state that doesn't exist for a kanji selection. */
body.kanji-active .char-info-static,
body.kanji-active .char-info-feedback,
body.kanji-active #card-stroke-count,
body.kanji-active .stroke-tooltip {
    display: none !important;
}

/* The carousel track holds the family-slide visuals — irrelevant
   for kanji which doesn't live in a family. The frame containing
   the canvas stays visible. */
body.kanji-active .carousel-track-wrapper { display: none !important; }

/* Recall is a kana-only flow — hide its button in the canvas header
   strip when kanji mode is active. The tagline + strip stay visible
   because the kanji writing canvas reuses both. */
body.kanji-active .canvas-header-strip .recall-wrap { display: none !important; }

/* SRS gauge bar to the left of the char-row is kana-only too
   (it queries SRS.v2 by family). Hide it when displaying kanji
   sublesson tiles so the row reads cleanly. */
body.kanji-active .char-row-srs-gauge { display: none !important; }

/* Kana char-mini tiles include a romaji label and per-gauge
   reading/writing dots. Kanji tiles re-use the same .char-mini
   class for layout consistency but only need the glyph; the
   .char-mini > .romaji and .gauge-row children are CSS-suppressed. */
body.kanji-active .char-mini .romaji,
body.kanji-active .char-mini .gauge-row {
    display: none !important;
}

/* Adjust kanji char-mini to be more vertical-centered without the
   romaji + gauge children — and use the JP font for the glyph. */
body.kanji-active .char-mini {
    justify-content: center;
}
body.kanji-active .char-mini .char {
    font-family: 'Noto Sans JP', sans-serif;
    font-size: 1.4rem;
}

/* Currently-active kanji in the char-row window. Matches the kana
   .char-mini.active treatment — transparent fill, accent outline,
   stronger glyph color — so the visual "you are here" language is
   the same across both modes. */
body.kanji-active .char-mini.selected {
    background: transparent;
    border-color: var(--accent);
}
body.kanji-active .char-mini.selected .char {
    color: var(--heading);
}

/* KUN-cloud sub-section inside the pen-dropdown — only visible when
   the user is actually practicing kanji. Mirrors grammar's
   .grammar-only-section visibility rule. */
body:not(.kanji-active) .kanji-only-section { display: none !important; }

/* ── No-stroke-data overlay ─────────────────────────────────────────
   Some kanji ship with a pathData silhouette but no per-stroke array
   yet — practicing is not possible until the strokes are drawn. When
   #card.no-strokes is set (by loadKanjiForCanvas), we fade the empty
   outline, show a warm "Coming soon" message centered on the canvas,
   and disable canvas-pointer events so half-attempted drags don't
   leave ink behind. Only fires in kanji mode — kana characters are
   never expected to be stroke-less. */
.kanji-no-strokes-overlay {
    position: absolute;
    inset: 0;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 10px;
    padding: 24px;
    text-align: center;
    color: var(--text-muted);
    pointer-events: none;
    opacity: 0;
    transition: opacity 0.25s ease;
    z-index: 3;
}
body.kanji-active #card.no-strokes .kanji-no-strokes-overlay {
    opacity: 1;
}
body.kanji-active #card.no-strokes #draw-canvas {
    opacity: 0.15;
    pointer-events: none;
}
.kanji-no-strokes-icon {
    font-size: 2.2rem;
    color: var(--accent);
    opacity: 0.7;
    line-height: 1;
}
.kanji-no-strokes-title {
    font-size: 0.98rem;
    font-weight: 600;
    color: var(--text);
    max-width: 320px;
    line-height: 1.4;
}
.kanji-no-strokes-sub {
    font-size: 0.85rem;
    color: var(--text-faint);
    max-width: 320px;
    line-height: 1.4;
    font-style: italic;
}

/* Kana-only view options (Audio on click, 3-column layout) in the All
   Characters column dropdown — hidden in kanji mode because neither
   applies to the lesson-grouped kanji list. Highlight flagged stays
   visible in both modes. */
body.kanji-active .kana-only-view-option { display: none !important; }

/* Flag-filter visual treatment for kanji tiles — mirrors the kana
   grid's flag-filter pattern: when the user has Highlight flagged on,
   non-flagged tiles dim to 0.45 so flagged ones pop. The flagged tile
   also picks up a red border to match the kana convention. */
body.flag-filter-active .kanji-tile { opacity: 0.45; transition: opacity 0.2s; }
body.flag-filter-active .kanji-tile.flagged { opacity: 1; }
.kanji-tile.flagged { border-color: var(--red); }

/* ── Custom lesson section header (Lessons view) ────────── */
/* Flex row so the "×" delete affordance can sit at the right edge.
   Wraps the existing header styling — same font/color/etc. */
.kanji-section-header.kanji-section-custom {
    display: flex;
    align-items: center;
    gap: 8px;
}
.kanji-section-header.kanji-section-custom .kanji-section-name {
    flex: 1;
}
.kanji-section-delete {
    width: 18px;
    height: 18px;
    border: none;
    background: transparent;
    color: var(--text-faint);
    font-size: 1rem;
    line-height: 1;
    cursor: pointer;
    border-radius: 4px;
    transition: color 0.12s, background 0.12s;
    padding: 0;
}
@media (hover: hover) {
    .kanji-section-delete:hover {
        color: var(--red, var(--accent));
        background: var(--accent-bg);
    }
}

/* ── Details panel "+ Add to Lesson" button ──────────────── */
/* Mirrors .vocab-detail-add-btn — left-justified within the details
   column, soft-dashed neutral border, muted dark text at rest,
   accent color on hover. Same visual language so the affordance
   reads as "the same kind of save action" across modes. */
.kanji-details-add-row {
    padding: 10px 0 4px;
}

.kanji-details-add-btn {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    padding: 7px 16px;
    border: var(--dash-soft);
    border-radius: 8px;
    background: transparent;
    color: var(--text-muted);
    font: inherit;
    font-size: 0.8rem;
    font-weight: 600;
    letter-spacing: 0.04em;
    white-space: nowrap;
    cursor: pointer;
    transition: border-color 0.15s, color 0.15s, background 0.15s;
}
@media (hover: hover) {
    .kanji-details-add-btn:hover {
        border: 1.5px solid var(--accent);
        color: var(--accent);
        background: var(--accent-bg);
    }
}
.kanji-details-add-plus {
    font-size: 1rem;
    line-height: 1;
}

/* ── Add-to-Lesson popover ───────────────────────────────── */
/* Fixed-position floater, anchored to the "+" button on the kanji
   tile that opened it. Width caps so a long lesson name doesn't blow
   it out; scroll if the user has many custom lessons. */
.kanji-add-popover {
    background: var(--bg);
    border: 1.5px solid var(--card-outline, var(--subtle-border));
    border-radius: 12px;
    box-shadow: 0 16px 40px rgba(0, 0, 0, 0.22);
    padding: 12px;
    min-width: 200px;
    max-width: 280px;
    z-index: 1700;
    display: flex;
    flex-direction: column;
    gap: 8px;
    font-size: 0.9rem;
    color: var(--text);
}

.kanji-pop-title {
    font-size: 0.78rem;
    font-weight: 700;
    letter-spacing: 0.06em;
    text-transform: uppercase;
    color: var(--text-faint);
    padding-bottom: 4px;
}

.kanji-pop-lessons {
    display: flex;
    flex-direction: column;
    gap: 4px;
    max-height: 220px;
    overflow-y: auto;
}

.kanji-pop-lesson {
    display: flex;
    align-items: center;
    gap: 8px;
    text-align: left;
    padding: 8px 10px;
    border: 1px solid var(--subtle-border);
    border-radius: 8px;
    background: var(--bg);
    color: var(--text);
    font-size: 0.92rem;
    cursor: pointer;
    transition: background 0.12s, border-color 0.12s;
}
@media (hover: hover) {
    .kanji-pop-lesson:not(:disabled):hover {
        background: var(--accent-bg);
        border-color: var(--accent);
    }
}
.kanji-pop-lesson:disabled {
    opacity: 0.55;
    cursor: default;
}
.kanji-pop-lesson-name {
    flex: 1;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}
.kanji-pop-lesson-check {
    color: var(--accent);
    font-weight: 700;
}

.kanji-pop-new {
    display: flex;
    align-items: center;
    gap: 6px;
    padding: 8px 10px;
    border: 1.5px dashed var(--accent);
    border-radius: 8px;
    background: transparent;
    color: var(--accent);
    font-size: 0.88rem;
    font-weight: 600;
    cursor: pointer;
    transition: background 0.12s;
}
@media (hover: hover) {
    .kanji-pop-new:hover { background: var(--accent-bg); }
}
.kanji-pop-plus {
    font-size: 1.1rem;
    line-height: 1;
}

.kanji-pop-input {
    width: 100%;
    box-sizing: border-box;
    padding: 8px 10px;
    border: 1px solid var(--subtle-border);
    border-radius: 8px;
    background: var(--bg);
    color: var(--text);
    font-family: inherit;
    font-size: 0.92rem;
    outline: none;
    transition: border-color 0.12s;
}
.kanji-pop-input:focus { border-color: var(--accent); }

.kanji-pop-actions {
    display: flex;
    justify-content: flex-end;
    gap: 6px;
}
.kanji-pop-cancel,
.kanji-pop-create {
    padding: 6px 14px;
    border-radius: 6px;
    font-size: 0.85rem;
    font-weight: 600;
    cursor: pointer;
    border: 1px solid var(--subtle-border);
    background: var(--bg);
    color: var(--text);
    transition: background 0.12s, border-color 0.12s, color 0.12s;
}
.kanji-pop-create {
    border-color: var(--accent);
    background: var(--accent);
    color: #fff;
}
@media (hover: hover) {
    .kanji-pop-cancel:hover { border-color: var(--text); }
    .kanji-pop-create:hover { filter: brightness(0.95); }
}
