/* ══════════════════════════════════════════
   KOERU · Học Kanji — study.css  v2
   ══════════════════════════════════════════ */

/* ── Reset ── */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --bg:      #0a0a1c;
  --surface: #12122a;
  --card:    #17173a;
  --border:  rgba(255,255,255,.09);
  --text:    #f0f0f8;
  --muted:   rgba(255,255,255,.45);
  --accent:  #6366f1;
  --gold:    #f59e0b;
  --green:   #22c55e;
  --sat: env(safe-area-inset-top,0px);
  --sab: env(safe-area-inset-bottom,0px);
}

html, body { height: 100%; }
body {
  background: var(--bg);
  color: var(--text);
  font-family: 'Plus Jakarta Sans','Be Vietnam Pro',system-ui,sans-serif;
  font-size: 15px;
  overflow-x: hidden;
}

/* ══════════════════════════════════════════
   HEADER
   ══════════════════════════════════════════ */
#study-header {
  position: sticky; top: 0; z-index: 200;
  background: rgba(10,10,28,.95);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  border-bottom: 1px solid var(--border);
  padding: calc(10px + var(--sat)) 16px 10px;
  display: flex; align-items: center; gap: 10px;
  height: 54px;
}
#study-back {
  text-decoration: none; color: var(--muted);
  font-size: .72rem; font-weight: 700;
  padding: 5px 10px; border-radius: 8px;
  border: 1px solid var(--border);
  background: rgba(255,255,255,.04);
  white-space: nowrap;
  transition: color .15s, background .15s;
}
#study-back:hover { color: var(--text); background: rgba(255,255,255,.09); }
#study-header-title {
  flex: 1; font-size: .95rem; font-weight: 800; letter-spacing: -.01em;
}
#study-goto-game {
  padding: 6px 14px;
  background: linear-gradient(135deg,#6366f1,#8b5cf6);
  border: none; border-radius: 16px;
  color: #fff; font-size: .75rem; font-weight: 700;
  cursor: pointer; white-space: nowrap;
  transition: opacity .15s;
}
#study-goto-game:hover { opacity: .85; }

/* ══════════════════════════════════════════
   PAGE LAYOUT (below header)
   ══════════════════════════════════════════ */
#study-page {
  display: flex;
  /* Mobile: column. Desktop: row — handled in media query */
  flex-direction: column;
  height: calc(100dvh - 54px);
  overflow: hidden;
}

/* ── Left column ── */
#study-left {
  flex: 1 1 0;
  min-width: 0;               /* critical: allow flex child to shrink */
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

/* ── Toolbar (level tabs + search) ── */
#study-toolbar {
  padding: 10px 14px 8px;
  border-bottom: 1px solid var(--border);
  display: flex; flex-direction: column; gap: 8px;
  flex-shrink: 0;
  background: var(--bg);
}

#level-tabs {
  display: flex; gap: 6px; flex-wrap: wrap;
}
.lvl-tab {
  padding: 5px 14px; border-radius: 18px;
  border: 1px solid var(--border);
  background: rgba(255,255,255,.04);
  color: var(--muted); font-size: .74rem; font-weight: 700;
  cursor: pointer; transition: all .15s; user-select: none;
}
.lvl-tab small { opacity: .6; font-size: .68rem; }
.lvl-tab:hover { color: var(--text); background: rgba(255,255,255,.08); }
.lvl-tab.active { background: var(--accent); border-color: var(--accent); color: #fff; }
.lvl-coming-soon { opacity: .38 !important; cursor: not-allowed !important; pointer-events: none !important; }
.lvl-coming-soon::after { content: " 🔒"; font-size: .65em; opacity: .7; }

#study-search-wrap { position: relative; }
#study-search {
  width: 100%; padding: 8px 32px 8px 12px;
  background: rgba(255,255,255,.06);
  border: 1px solid var(--border); border-radius: 9px;
  color: var(--text); font-size: .82rem; outline: none;
  transition: border-color .15s;
}
#study-search::placeholder { color: var(--muted); }
#study-search:focus { border-color: rgba(99,102,241,.5); }
#study-search-clear {
  position: absolute; right: 9px; top: 50%;
  transform: translateY(-50%);
  background: none; border: none;
  color: var(--muted); font-size: .85rem; cursor: pointer;
  display: none; line-height: 1; padding: 2px;
}

/* ── Stats bar ── */
#study-stats {
  display: flex; align-items: center;
  justify-content: space-between; gap: 8px;
  padding: 5px 14px 2px;
  flex-shrink: 0;
}
#study-stats-count { font-size: .7rem; font-weight: 700; color: rgba(255,255,255,.5); }
#study-legend {
  display: flex; gap: 10px;
}
.legend-item {
  display: flex; align-items: center; gap: 4px;
  font-size: .64rem; color: var(--muted);
}
.legend-dot { width: 7px; height: 7px; border-radius: 50%; }

/* ── Grid area (scrollable) ── */
#kanji-grid-wrap {
  flex: 1 1 0;
  overflow-y: auto;
  padding: 10px 12px calc(16px + var(--sab));
  /* width: 100% inherited from flex parent */
}
#kanji-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(80px, 1fr));
  gap: 7px;
  width: 100%;
}

/* ── Kanji cell ── */
.k-cell {
  background: var(--card);
  border: 1.5px solid var(--border);
  border-radius: 10px;
  padding: 8px 6px 7px;
  cursor: pointer;
  text-align: center;
  transition: border-color .15s, background .15s, transform .1s;
  position: relative;
  user-select: none;
  -webkit-tap-highlight-color: transparent;
  animation: kfadeIn .14s ease both;
}
.k-cell:hover  { border-color: rgba(99,102,241,.5); background: rgba(99,102,241,.1); }
.k-cell:active { transform: scale(.93); }
.k-cell.selected {
  border-color: var(--accent); background: rgba(99,102,241,.16);
  box-shadow: 0 0 0 2px rgba(99,102,241,.28);
}
.k-cell.seen    .k-dot { background: var(--gold); }
.k-cell.learned .k-dot { background: var(--green); }

.k-dot {
  position: absolute; top: 5px; right: 5px;
  width: 5px; height: 5px; border-radius: 50%;
}

.k-char {
  font-family: 'Noto Serif JP', serif;
  font-size: 1.75rem; font-weight: 900;
  line-height: 1.1; color: #fff;
  display: block; margin-bottom: 3px;
}
.k-hv {
  font-size: .58rem; font-weight: 700; color: var(--gold);
  letter-spacing: .04em; text-transform: uppercase;
  display: block; margin-bottom: 1px;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.k-mean {
  font-size: .58rem; color: var(--muted);
  display: block; overflow: hidden;
  text-overflow: ellipsis; white-space: nowrap;
  margin-bottom: 4px;
}
.k-level {
  display: inline-block; padding: 1px 5px;
  border-radius: 5px; font-size: .55rem; font-weight: 700;
}
.lv-N5 { background: rgba(16,185,129,.18); color: #6ee7b7; }
.lv-N4 { background: rgba(99,102,241,.18); color: #a5b4fc; }
.lv-N3 { background: rgba(245,158,11,.18); color: #fcd34d; }
.lv-N2 { background: rgba(239,68,68,.18);  color: #fca5a5; }
.lv-N1 { background: rgba(168,85,247,.18); color: #d8b4fe; }

/* empty state */
#study-empty {
  display: none; flex-direction: column;
  align-items: center; justify-content: center;
  padding: 60px 24px; color: var(--muted);
  text-align: center; gap: 10px;
}
#study-empty .empty-icon { font-size: 2.4rem; }
#study-empty p { font-size: .82rem; line-height: 1.6; }

/* ══════════════════════════════════════════
   DETAIL PANEL — mobile: bottom sheet
   ══════════════════════════════════════════ */
#detail-backdrop {
  display: none; position: fixed; inset: 0;
  z-index: 299; background: rgba(0,0,0,.55);
}
#detail-backdrop.show { display: block; }

#detail-panel {
  position: fixed; bottom: 0; left: 0; right: 0;
  /* needed for #detail-close absolute positioning */
  z-index: 300;
  background: #13132e;
  border-top: 1px solid rgba(255,255,255,.12);
  border-radius: 20px 20px 0 0;
  padding-bottom: calc(12px + var(--sab));
  max-height: 78dvh;
  overflow-y: auto;
  transform: translateY(100%);
  transition: transform .26s cubic-bezier(.32,0,.16,1);
  overscroll-behavior: contain;
}
#detail-panel.open { transform: translateY(0); }

/* close button */
#detail-close {
  position: absolute; top: 10px; right: 12px;
  width: 28px; height: 28px;
  background: rgba(255,255,255,.08);
  border: 1px solid rgba(255,255,255,.14);
  border-radius: 50%;
  color: rgba(255,255,255,.7);
  font-size: 13px; line-height: 1;
  cursor: pointer;
  display: flex; align-items: center; justify-content: center;
  transition: background .15s;
  z-index: 10;
}
#detail-close:hover { background: rgba(255,255,255,.18); color: #fff; }

/* drag handle */
#detail-handle {
  width: 34px; height: 4px;
  background: rgba(255,255,255,.14); border-radius: 2px;
  margin: 10px auto 12px;
}

/* ── Top section (kanji + meta) ── */
#detail-top {
  display: flex; gap: 14px;
  padding: 0 18px 14px;
  border-bottom: 1px solid var(--border);
}
#detail-kanji-big {
  font-family: 'Noto Serif JP', serif;
  font-size: 4.8rem; font-weight: 900;
  line-height: 1; color: #fff; flex-shrink: 0;
  width: 80px; text-align: center;
}
#detail-meta { flex: 1; min-width: 0; padding-top: 4px; }
#detail-hanviet {
  font-size: 1rem; font-weight: 800; color: var(--gold);
  letter-spacing: .06em; text-transform: uppercase;
  margin-bottom: 3px;
}
#detail-meaning {
  font-size: .85rem; color: rgba(255,255,255,.8);
  margin-bottom: 6px; line-height: 1.45;
}
.reading-row { font-size: .73rem; color: var(--muted); line-height: 1.7; }
.reading-row b { color: rgba(255,255,255,.65); font-weight: 700; margin-right: 4px; }
#detail-badges {
  display: flex; gap: 5px; flex-wrap: wrap; margin-top: 7px;
}
.detail-badge {
  padding: 2px 7px; border-radius: 5px;
  font-size: .62rem; font-weight: 700;
  background: rgba(255,255,255,.07); color: rgba(255,255,255,.45);
}
.detail-badge.lv-N5 { background: rgba(16,185,129,.14); color: #6ee7b7; }
.detail-badge.lv-N4 { background: rgba(99,102,241,.14); color: #a5b4fc; }
.detail-badge.lv-N3 { background: rgba(245,158,11,.14); color: #fcd34d; }
.detail-badge.lv-N2 { background: rgba(239,68,68,.14);  color: #fca5a5; }
.detail-badge.lv-N1 { background: rgba(168,85,247,.14); color: #d8b4fe; }
.detail-badge.freq  { background: rgba(245,158,11,.14); color: #fcd34d; }

/* ── Stroke order ── */
#detail-stroke-wrap {
  padding: 12px 18px;
  border-bottom: 1px solid var(--border);
}
.detail-section-title {
  font-size: .65rem; font-weight: 700;
  letter-spacing: .08em; text-transform: uppercase;
  color: var(--muted); margin-bottom: 10px;
}
#detail-stroke-inner {
  display: flex; align-items: center; gap: 14px;
}
#detail-stroke-svg {
  width: 96px; height: 96px; flex-shrink: 0;
  border-radius: 10px; border: 1px solid var(--border);
  background: #08081a;
  display: flex; align-items: center; justify-content: center;
  overflow: hidden;
}
#detail-stroke-svg svg {
  width: 86px; height: 86px; display: block;
}
#detail-stroke-svg svg path  { stroke: #a5b4fc !important; }
#detail-stroke-svg svg circle,
#detail-stroke-svg svg ellipse { fill: #6366f1 !important; }

#detail-stroke-info { flex: 1; }
#detail-stroke-count { font-size: .74rem; color: var(--muted); margin-bottom: 7px; }
#btn-replay-stroke {
  padding: 6px 12px;
  background: rgba(99,102,241,.16);
  border: 1px solid rgba(99,102,241,.3);
  border-radius: 7px; color: #a5b4fc;
  font-size: .72rem; font-weight: 700; cursor: pointer;
  transition: background .15s;
}
#btn-replay-stroke:hover { background: rgba(99,102,241,.28); }
.stroke-load-msg { font-size: .72rem; color: var(--muted); text-align: center; line-height: 1.5; }
.stroke-attr { margin-top: 7px; font-size: .6rem; color: rgba(255,255,255,.18); }

/* ── Vocabulary ── */
#detail-vocab-wrap {
  padding: 12px 18px;
  border-bottom: 1px solid var(--border);
}
#detail-vocab-list {
  display: flex; flex-direction: column; gap: 6px;
}
.vocab-item {
  display: flex; align-items: baseline; gap: 8px;
  padding: 8px 11px;
  background: rgba(255,255,255,.04);
  border: 1px solid var(--border); border-radius: 9px;
}
.vocab-w {
  font-family: 'Noto Serif JP', serif;
  font-size: 1rem; font-weight: 700; color: #fff; flex-shrink: 0;
}
.vocab-r { font-size: .7rem; color: rgba(255,255,255,.4); flex-shrink: 0; }
.vocab-m { font-size: .76rem; color: rgba(255,255,255,.6); flex: 1; }

/* ── Actions ── */
#detail-actions {
  padding: 12px 18px 0;
  display: flex; gap: 8px; flex-wrap: wrap;
}
.detail-action-btn {
  flex: 1; min-width: 130px; padding: 10px 12px;
  border-radius: 9px; border: none;
  font-size: .8rem; font-weight: 700; cursor: pointer;
  transition: opacity .15s; text-align: center;
  white-space: nowrap;
}
.detail-action-primary {
  background: linear-gradient(135deg,#6366f1,#8b5cf6); color: #fff;
}
.detail-action-secondary {
  background: rgba(255,255,255,.07);
  border: 1px solid var(--border); color: rgba(255,255,255,.65);
}
.detail-action-btn:hover { opacity: .82; }

/* ══════════════════════════════════════════
   DESKTOP ≥ 900px — side panel
   ══════════════════════════════════════════ */
@media (min-width: 900px) {
  body { overflow: hidden; }

  #study-page {
    flex-direction: row;  /* grid on left, panel on right */
    height: calc(100dvh - 54px);
    overflow: hidden;
  }

  /* Left: scrollable grid */
  #study-left {
    flex: 1 1 0;
    min-width: 0;
    overflow: hidden;
    display: flex; flex-direction: column;
  }
  #kanji-grid-wrap {
    flex: 1 1 0;
    overflow-y: auto;
    padding: 12px 16px 24px;
    min-height: 0;   /* required for overflow-y scroll in flex child */
  }
  #kanji-grid {
    grid-template-columns: repeat(auto-fill, minmax(90px, 1fr));
    gap: 9px;
  }
  .k-char { font-size: 1.85rem; }

  /* Right: fixed-width panel, always visible */
  #detail-panel {
    position: static;
    width: 360px; flex-shrink: 0;
    border-radius: 0; border-top: none;
    border-left: 1px solid var(--border);
    max-height: none; height: 100%;
    transform: none !important;
    overflow-y: auto;
    display: none; flex-direction: column;
    background: #10102a;
    /* Show as flex when open */
  }
  #detail-panel.open { display: flex; }
  /* On desktop no backdrop needed */
  #detail-backdrop { display: none !important; }
  #detail-handle { display: none; }

  /* Placeholder when no kanji selected */
  #detail-placeholder {
    display: flex; flex-direction: column;
    align-items: center; justify-content: center;
    height: 100%; color: var(--muted); text-align: center;
    gap: 10px; padding: 32px;
  }
  #detail-placeholder .ph-icon { font-size: 2.8rem; opacity: .4; }
  #detail-placeholder p { font-size: .8rem; line-height: 1.6; }

  #detail-top    { padding: 18px 20px 14px; }
  #detail-stroke-wrap { padding: 12px 20px; }
  #detail-vocab-wrap  { padding: 12px 20px; }
  #detail-actions     { padding: 12px 20px 0; }
}

@media (min-width: 1100px) {
  #detail-panel { width: 400px; }
  #kanji-grid {
    grid-template-columns: repeat(auto-fill, minmax(96px, 1fr));
  }
}

@media (min-width: 1400px) {
  #kanji-grid {
    grid-template-columns: repeat(auto-fill, minmax(104px, 1fr));
  }
}

/* ── Mobile portrait optimization ── */
@media (max-width: 400px) {
  #kanji-grid { grid-template-columns: repeat(4, 1fr); gap: 6px; }
  .k-char { font-size: 1.5rem; }
}

/* ── Scrollbar ── */
::-webkit-scrollbar { width: 4px; height: 4px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: rgba(255,255,255,.1); border-radius: 2px; }

/* ── Fade-in animation for cells ── */
@keyframes kfadeIn {
  from { opacity: 0; transform: scale(.92); }
  to   { opacity: 1; transform: none; }
}

/* ── Radical & Parts ── */
#detail-radical-wrap,
#detail-mnemonic-wrap {
  padding: 12px 16px;
  border-top: 1px solid rgba(255,255,255,.06);
}
#detail-radical-row,
#detail-parts-row {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-top: 6px;
}
#detail-parts-row { margin-top: 8px; }
.detail-sub-label {
  font-size: .68rem;
  color: var(--muted);
  width: 64px;
  flex-shrink: 0;
  text-transform: uppercase;
  letter-spacing: .04em;
}
.rad-char {
  font-size: 1.4rem;
  font-family: 'Noto Serif JP', serif;
  color: #c4b5fd;
  line-height: 1;
}
.rad-info {
  font-size: .75rem;
  color: rgba(255,255,255,.55);
}
.part-char {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 30px; height: 30px;
  background: rgba(165,180,252,.1);
  border: 1px solid rgba(165,180,252,.2);
  border-radius: 6px;
  font-size: 1rem;
  font-family: 'Noto Serif JP', serif;
  color: #a5b4fc;
  cursor: default;
  transition: background .15s;
}
.part-char:hover { background: rgba(165,180,252,.2); }

/* ── Mnemonic ── */
#detail-mnemonic {
  font-size: .8rem;
  color: rgba(255,255,255,.6);
  line-height: 1.55;
  font-style: italic;
  margin-top: 6px;
  padding: 8px 10px;
  background: rgba(251,191,36,.06);
  border-left: 2px solid rgba(251,191,36,.4);
  border-radius: 0 6px 6px 0;
}
#detail-mnemonic .note {
  font-size: .72rem;
  color: rgba(255,255,255,.4);
  font-style: italic;
}

/* ── Grade badge ── */
.detail-badge.grade {
  background: rgba(34,197,94,.15);
  color: #4ade80;
  border-color: rgba(34,197,94,.2);
}
