/* ============================================================================
Matrix Hanzi Pro - Workspace Optimized UI
Mục tiêu:
- Nhẹ, sạch, ít hiệu ứng nặng.
- Tối ưu điện thoại khi bàn phím ảo mở.
- Tối ưu sáng/tối, đọc rõ, nhập đáp án nhanh.
- Giữ nguyên HTML id/class để không phá JS hiện tại.
============================================================================ */

:root {
  color-scheme: dark;
  --font-ui: 'DM Sans', -apple-system, BlinkMacSystemFont, 'Segoe UI', system-ui, sans-serif;
  --font-mono: 'DM Mono', ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  --font-hanzi: 'Noto Serif SC', serif;

  --bg-main: #07111d;
  --bg-layer: #0b1624;
  --bg-card: rgba(13, 24, 38, 0.92);
  --bg-card-hover: rgba(18, 34, 52, 0.96);
  --bg-input: rgba(7, 16, 28, 0.94);
  --bg-panel: rgba(10, 20, 32, 0.96);

  --border-color: rgba(130, 183, 218, 0.22);
  --border-subtle: rgba(255, 255, 255, 0.08);
  --text-primary: #f6f9ff;
  --text-secondary: #b6c6d8;
  --text-muted: #768aa0;

  --accent-neon: #18b7ff;
  --accent-green: #34d399;
  --accent-orange: #fbbf24;
  --accent-red: #fb7185;
  --accent-neon-dim: rgba(24, 183, 255, 0.12);
  --accent-green-dim: rgba(52, 211, 153, 0.12);
  --accent-orange-dim: rgba(251, 191, 36, 0.12);
  --accent-red-dim: rgba(251, 113, 133, 0.13);

  --radius-sm: 10px;
  --radius-md: 14px;
  --radius-lg: 18px;
  --shadow-card: 0 10px 28px rgba(0, 0, 0, 0.22);
  --shadow-popup: 0 22px 58px rgba(0, 0, 0, 0.48);

  --nav-h: 68px;
  --mobile-bottom-gap: env(safe-area-inset-bottom, 0px);
}

body.light-mode {
  color-scheme: light;
  --bg-main: #f5f0e6;
  --bg-layer: #fbf8ef;
  --bg-card: rgba(255, 253, 247, 0.94);
  --bg-card-hover: rgba(247, 243, 232, 0.98);
  --bg-input: rgba(255, 255, 255, 0.96);
  --bg-panel: rgba(248, 245, 236, 0.96);

  --border-color: rgba(21, 94, 117, 0.20);
  --border-subtle: rgba(15, 23, 42, 0.08);
  --text-primary: #0f172a;
  --text-secondary: #475569;
  --text-muted: #7a8aa0;

  --accent-neon: #0284c7;
  --accent-green: #059669;
  --accent-orange: #d97706;
  --accent-red: #e11d48;
  --accent-neon-dim: rgba(2, 132, 199, 0.10);
  --accent-green-dim: rgba(5, 150, 105, 0.10);
  --accent-orange-dim: rgba(217, 119, 6, 0.10);
  --accent-red-dim: rgba(225, 29, 72, 0.10);

  --shadow-card: 0 8px 22px rgba(15, 23, 42, 0.07);
  --shadow-popup: 0 18px 46px rgba(15, 23, 42, 0.16);
}

* { box-sizing: border-box; margin: 0; padding: 0; }
html, body { min-height: 100%; overflow-x: hidden; }
html { background: var(--bg-main); }
body {
  background:
    radial-gradient(circle at 12% 0%, rgba(24, 183, 255, 0.11), transparent 28%),
    radial-gradient(circle at 88% 15%, rgba(52, 211, 153, 0.07), transparent 24%),
    linear-gradient(135deg, #030811, var(--bg-main) 55%, #030811);
  color: var(--text-primary);
  font-family: var(--font-ui);
  font-size: 15px;
  line-height: 1.45;
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
}
body.light-mode {
  background:
    radial-gradient(circle at 12% 0%, rgba(2, 132, 199, 0.10), transparent 28%),
    radial-gradient(circle at 85% 18%, rgba(13, 148, 136, 0.08), transparent 26%),
    linear-gradient(135deg, #f3eddf, var(--bg-main) 54%, #eef7f6);
}

.workspace-bg-art { position: fixed; inset: 0; z-index: -1; overflow: hidden; pointer-events: none; }
.bg-calligraphy { position: absolute; font-family: var(--font-hanzi); font-weight: 900; font-size: clamp(160px, 25vw, 360px); line-height: 1; color: rgba(255,255,255,0.035); user-select: none; }
.bg-calligraphy-left { left: -5vw; top: 12vh; transform: rotate(-10deg); }
.bg-calligraphy-right { right: -4vw; bottom: 4vh; transform: rotate(8deg); }
body.light-mode .bg-calligraphy { color: rgba(15, 23, 42, 0.045); }
.bg-orb { position: absolute; border-radius: 50%; filter: blur(28px); opacity: .35; }
.bg-orb-one { width: 280px; height: 180px; left: 14%; top: 68%; background: rgba(24,183,255,.18); }
.bg-orb-two { width: 240px; height: 180px; right: 8%; top: 8%; background: rgba(52,211,153,.12); }
body.light-mode .bg-orb { opacity: .16; }

.screen { display: none; min-height: 100dvh; }
.screen.active { display: block; }
.hidden-col { display: none !important; }

/* Layout */
.workspace-layout {
  display: flex;
  flex-direction: column;
  min-height: 100dvh;
  height: 100dvh;
  overflow: hidden;
}

.top-navbar {
  flex: 0 0 auto;
  min-height: var(--nav-h);
  padding: 12px clamp(14px, 2vw, 24px);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  background: var(--bg-card);
  border-bottom: 1px solid var(--border-color);
  box-shadow: var(--shadow-card);
  position: relative;
  z-index: 70;
}

.top-navbar > div:first-child { min-width: 0; }
#status { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.nav-controls-wrapper { position: relative; display: flex; align-items: center; gap: 12px; }
.mobile-collapse-menu { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }
.toggle-mobile-menu-btn { display: none; }
.desktop-menu-label { display: inline-flex; }

.sub-panel {
  flex: 0 0 auto;
  padding: 10px clamp(14px, 2vw, 24px);
  background: var(--bg-panel);
  border-bottom: 1px solid var(--border-color);
  display: flex;
  align-items: center;
  gap: 14px;
  flex-wrap: wrap;
}
.sub-panel-read-mode { margin-left: auto; display: flex; align-items: center; gap: 12px; }
.mobile-only-options, .collapse-subpanel-slot { display: flex; flex-direction: column; gap: 12px; padding-top: 12px; border-top: 1px solid var(--border-subtle); }
.sub-panel-label { font-size: 11px; color: var(--text-muted); font-weight: 900; text-transform: uppercase; letter-spacing: .08em; }
.sub-panel-checkbox { display: inline-flex; align-items: center; gap: 7px; color: var(--text-secondary); font-size: 13px; font-weight: 800; cursor: pointer; }
.sub-panel input[type="checkbox"], .mobile-collapse-menu input[type="checkbox"] { width: 16px; height: 16px; accent-color: var(--accent-neon); }
.sub-panel select { width: auto; min-width: 150px; padding: 7px 12px; border-radius: 10px; font-size: 13px; }

.table-container {
  flex: 1 1 auto;
  min-height: 0;
  overflow: auto;
  padding: 18px clamp(14px, 2vw, 24px);
  -webkit-overflow-scrolling: touch;
  overscroll-behavior: contain;
  scrollbar-gutter: stable;
}

/* Controls */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  min-height: 40px;
  padding: 10px 16px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border-color);
  background: var(--bg-input);
  color: var(--text-primary);
  font: 800 13.5px/1 var(--font-ui);
  cursor: pointer;
  white-space: nowrap;
  outline: none;
  box-shadow: none;
  transition: background-color .16s ease, border-color .16s ease, transform .16s ease;
}
.btn:hover { background: var(--bg-card-hover); border-color: var(--accent-neon); }
.btn:active { transform: translateY(1px); }
.btn-primary { background: linear-gradient(135deg, var(--accent-green), #0d9488); color: #ffffff; border-color: transparent; }
.btn-danger { background: var(--accent-red-dim); color: var(--accent-red); border-color: rgba(251,113,133,.28); }
.reset-data-btn { background: linear-gradient(135deg, #f97316, #f59e0b) !important; color: #fff !important; border-color: transparent !important; }

input[type="text"], select, .answer-input {
  width: 100%;
  background: var(--bg-input);
  border: 1px solid var(--border-color);
  color: var(--text-primary);
  border-radius: 12px;
  outline: none;
  font-family: var(--font-ui);
}
input[type="text"], select { padding: 10px 14px; font-size: 14px; font-weight: 700; }
input[type="text"]:focus, select:focus, .answer-input:focus {
  border-color: var(--accent-neon);
  box-shadow: 0 0 0 3px var(--accent-neon-dim);
}

/* Desktop table */
#vocabTable {
  width: 100%;
  border-collapse: separate;
  border-spacing: 0 8px;
  table-layout: fixed;
  min-width: 1120px;
}
#vocabTable th {
  padding: 9px 10px;
  color: var(--text-muted);
  font-size: 11px;
  font-weight: 900;
  text-transform: uppercase;
  letter-spacing: .06em;
  text-align: left;
  position: sticky;
  top: 0;
  z-index: 5;
  background: var(--bg-main);
}
body.light-mode #vocabTable th { background: var(--bg-main); }
#vocabTable td {
  background: var(--bg-card);
  padding: 10px;
  vertical-align: middle;
  border-top: 1px solid var(--border-subtle);
  border-bottom: 1px solid var(--border-subtle);
}
#vocabTable tr:hover td { background: var(--bg-card-hover); }
#vocabTable td:first-child { border-radius: 12px 0 0 12px; border-left: 1px solid var(--border-subtle); text-align: center; font-family: var(--font-mono); color: var(--text-secondary); font-weight: 800; }
#vocabTable td:last-child { border-radius: 0 12px 12px 0; border-right: 1px solid var(--border-subtle); }
#vocabTable th:nth-child(1), #vocabTable td:nth-child(1) { width: 58px; }
#vocabTable th:nth-child(2), #vocabTable td:nth-child(2) { width: 68px; text-align: center; }
#vocabTable th:nth-child(3), #vocabTable td:nth-child(3) { width: 130px; text-align: center; }
#vocabTable th:nth-child(4), #vocabTable td:nth-child(4) { width: 145px; }
#vocabTable th:nth-child(5), #vocabTable td:nth-child(5) { width: 220px; }
#vocabTable th:nth-child(6), #vocabTable td:nth-child(6) { width: 260px; }
#vocabTable th:nth-child(7), #vocabTable td:nth-child(7) { width: 140px; text-align: center; }
#vocabTable th:nth-child(8), #vocabTable td:nth-child(8),
#vocabTable th:nth-child(9), #vocabTable td:nth-child(9) { width: 52px; text-align: center; }
#vocabTable td:nth-child(3) { font-family: var(--font-hanzi); font-size: 27px; font-weight: 900; }
#vocabTable td:nth-child(4) { color: var(--accent-neon); font-family: var(--font-mono); font-size: 14px; font-weight: 700; }
#vocabTable td:nth-child(5) { color: var(--accent-orange); font-size: 15px; font-weight: 800; line-height: 1.38; }
#vocabTable tbody tr { content-visibility: auto; contain-intrinsic-size: auto 62px; }

.answer-input {
  display: block;
  height: 42px;
  padding: 9px 12px;
  font-family: var(--font-hanzi);
  font-size: 17px;
  font-weight: 800;
  caret-color: var(--accent-neon);
}
.status-cell { display: block; width: 100%; padding: 8px 10px; border-radius: 10px; font-size: 12px; font-weight: 900; text-align: center; background: rgba(113,134,156,.12); color: var(--text-secondary); border: 1px solid var(--border-subtle); }
.status-cell.correct { background: var(--accent-green-dim) !important; color: var(--accent-green) !important; border-color: rgba(52,211,153,.28) !important; }
.status-cell.wrong { background: var(--accent-red-dim) !important; color: var(--accent-red) !important; border-color: rgba(251,113,133,.28) !important; }
.status-cell.pending { background: var(--accent-orange-dim) !important; color: var(--accent-orange) !important; border-color: rgba(251,191,36,.28) !important; }

/* FIX CHỌN THẺ THƯỜNG: Chỉ hiện viền xanh neon, KHÔNG đổi màu nền */
#vocabTable tr.current-row td, #vocabTable tr.active-row td, #vocabTable tr.is-current td {
  background: var(--bg-card) !important;
  border-top-color: var(--accent-neon) !important;
  border-bottom-color: var(--accent-neon) !important;
}
#vocabTable tr.current-row td:first-child, #vocabTable tr.active-row td:first-child, #vocabTable tr.is-current td:first-child {
  border-left-color: var(--accent-neon) !important;
}
#vocabTable tr.current-row td:last-child, #vocabTable tr.active-row td:last-child, #vocabTable tr.is-current td:last-child {
  border-right-color: var(--accent-neon) !important;
}
#vocabTable tr.current-row, #vocabTable tr.active-row, #vocabTable tr.is-current {
  outline: 2px solid var(--accent-neon) !important;
  outline-offset: -2px;
}

tr.forgotten-row td { background: rgba(251,191,36,.06); }
tr.forgotten-row td:nth-child(3) { color: var(--accent-orange); }
.speaker-icon, .dict-icon { display: inline-flex; align-items: center; justify-content: center; width: 34px; height: 34px; border-radius: 10px; background: var(--bg-input); border: 1px solid var(--border-color); color: var(--text-primary); cursor: pointer; text-decoration: none; opacity: .9; }
.speaker-icon:hover, .dict-icon:hover { color: var(--accent-neon); background: var(--accent-neon-dim); }

/* Desktop menu popup */
@media (min-width: 769px) {
  .toggle-mobile-menu-btn { display: inline-flex; min-width: 122px; border-radius: 999px; }
  .mobile-collapse-menu {
    position: absolute;
    top: calc(100% + 12px);
    right: 0;
    z-index: 9000;
    width: min(860px, calc(100vw - 36px));
    display: grid;
    grid-template-columns: repeat(4, minmax(140px, 1fr));
    gap: 10px;
    padding: 14px;
    border-radius: 20px;
    border: 1px solid var(--border-color);
    background: var(--bg-card);
    box-shadow: var(--shadow-popup);
    opacity: 0;
    visibility: hidden;
    pointer-events: none;
  }
  .mobile-collapse-menu.show { opacity: 1; visibility: visible; pointer-events: auto; }
  .mobile-collapse-menu > *, .mobile-collapse-menu .btn, .mobile-collapse-menu select { width: 100%; }
  .mobile-only-options, .collapse-subpanel-slot { grid-column: 1 / -1; }
  .mobile-collapse-menu .sub-panel.inside-collapse-menu { width: 100%; }
}

/* Mobile keyboard-first layout */
@media (max-width: 768px) {
  :root { --nav-h: 64px; }
  body { font-size: 14px; }
  .workspace-layout {
    height: 100dvh;
    min-height: 100dvh;
    padding-top: var(--nav-h);
  }
  .top-navbar {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    min-height: var(--nav-h);
    padding: 10px 12px;
    gap: 8px;
  }
  .top-navbar > div:first-child { flex: 1 1 auto; gap: 8px !important; }
  #backToDashBtn { min-width: 44px; padding: 0 12px; }
  #status { max-width: 42vw; font-size: 12px !important; }
  .nav-controls-wrapper { flex: 0 0 auto; }
  .toggle-mobile-menu-btn {
    display: inline-flex;
    min-width: 118px;
    height: 42px;
    padding: 0 13px;
    border-radius: 999px;
  }
  .toggle-mobile-menu-btn.active .arrow-icon { transform: rotate(180deg); }
  .workspace-layout > .sub-panel { display: none; }
  .table-container {
    height: calc(100dvh - var(--nav-h));
    max-height: calc(100dvh - var(--nav-h));
    padding: 10px 10px calc(12px + var(--mobile-bottom-gap));
    overflow-x: hidden;
    overflow-y: auto;
    scroll-padding-top: 10px;
    scroll-padding-bottom: max(180px, 34dvh);
  }

  .mobile-collapse-menu {
    position: fixed;
    left: 10px;
    right: 10px;
    top: calc(var(--nav-h) + 8px);
    z-index: 9998;
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 9px;
    padding: 10px;
    border-radius: 18px;
    border: 1px solid var(--border-color);
    background: var(--bg-card);
    box-shadow: var(--shadow-popup);
    opacity: 0;
    visibility: hidden;
    pointer-events: none;
    max-height: min(460px, calc(100dvh - var(--nav-h) - 18px));
    overflow: auto;
  }
  .mobile-collapse-menu.show { opacity: 1; visibility: visible; pointer-events: auto; }
  .mobile-collapse-menu .btn { width: 100%; min-height: 44px; font-size: 12px; padding: 8px; }
  .mobile-only-options, .collapse-subpanel-slot { grid-column: 1 / -1; }
  .mobile-collapse-menu .sub-panel.inside-collapse-menu {
    width: 100%;
    padding: 0;
    border-bottom: 0;
    background: transparent;
    gap: 10px;
  }
  .mobile-collapse-menu .sub-panel-read-mode {
    width: 100%;
    margin-left: 0;
    justify-content: space-between;
  }
  .mobile-collapse-menu .sub-panel-checkbox { min-height: 34px; }

  #vocabTable, #vocabTable tbody { display: block; width: 100%; min-width: 0; }
  #vocabTable { border-spacing: 0; }
  #vocabTable thead { display: none !important; }
  #vocabTable tbody tr {
    display: grid;
    grid-template-columns: minmax(72px, .75fr) minmax(0, 1.25fr) 38px 38px;
    grid-template-areas:
      "stt    pinyin forgot forgot"
      "hanzi  meaning listen dict"
      "input  input  input  input"
      "result result result result";
    column-gap: 10px;
    row-gap: 7px;
    align-items: center;
    margin: 0 0 10px;
    padding: 12px;
    border-radius: 18px;
    border: 1px solid var(--border-color);
    background: var(--bg-card);
    box-shadow: 0 4px 14px rgba(0,0,0,.12);
    content-visibility: auto;
    contain-intrinsic-size: auto 178px;
  }
  body.light-mode #vocabTable tbody tr { box-shadow: 0 4px 14px rgba(15,23,42,.06); }
  #vocabTable tbody tr:hover { background: var(--bg-card); }
  #vocabTable tbody td {
    width: auto !important;
    min-height: 0;
    margin: 0;
    padding: 0 !important;
    border: 0 !important;
    background: transparent !important;
    text-align: left !important;
    position: static;
    display: flex;
    flex-direction: column;
    justify-content: center;
    line-height: 1.18;
    overflow-wrap: anywhere;
  }
  #vocabTable tbody td::before {
    display: block;
    margin: 0 0 3px;
    font-size: 10px;
    line-height: 1;
    color: var(--text-muted);
    font-weight: 900;
    text-transform: uppercase;
    letter-spacing: .04em;
  }
  #vocabTable tbody td:nth-child(1) { grid-area: stt; color: var(--text-secondary); font-family: var(--font-mono); font-size: 13px; font-weight: 800; }
  #vocabTable tbody td:nth-child(1)::before { content: "STT"; }
  #vocabTable tbody td:nth-child(2) { grid-area: forgot; align-items: flex-end; justify-content: flex-start; min-height: 24px; font-size: 0; }
  #vocabTable tbody td:nth-child(2)::before { content: "Quên"; font-size: 10px; }
  #vocabTable .forgot-checkbox { width: 17px; height: 17px; margin-top: 2px; accent-color: var(--accent-orange); }
  #vocabTable tbody td:nth-child(3) { grid-area: hanzi; font-size: 30px; line-height: 1; align-items: flex-start; }
  #vocabTable tbody td:nth-child(3)::before { content: "Hán tự"; }
  #vocabTable tbody td:nth-child(4) { grid-area: pinyin; color: var(--accent-neon); font-size: 14px; font-weight: 850; }
  #vocabTable tbody td:nth-child(4)::before { content: "Pinyin"; }
  #vocabTable tbody td:nth-child(5) { grid-area: meaning; color: var(--accent-orange); font-size: 19px; font-weight: 900; line-height: 1.2; }
  #vocabTable tbody td:nth-child(5)::before { content: "Nghĩa"; }
  #vocabTable tbody td:nth-child(6) { grid-area: input; margin-top: 4px; }
  #vocabTable tbody td:nth-child(6)::before { content: ""; display: none; }
  #vocabTable .answer-input {
    height: 44px;
    min-height: 44px;
    padding: 8px 12px;
    border-radius: 13px;
    font-size: 16px;
  }
  #vocabTable tbody td:nth-child(7) { grid-area: result; }
  #vocabTable tbody td:nth-child(7)::before { content: ""; display: none; }
  #vocabTable .status-cell { text-align: left; padding: 7px 10px; border-radius: 11px; font-size: 12px; }
  #vocabTable tbody td:nth-child(8) { grid-area: listen; align-items: center; justify-content: center; }
  #vocabTable tbody td:nth-child(9) { grid-area: dict; align-items: center; justify-content: center; }
  #vocabTable tbody td:nth-child(8)::before, #vocabTable tbody td:nth-child(9)::before { content: ""; display: none; }
  #vocabTable .speaker-icon, #vocabTable .dict-icon { width: 36px; height: 36px; min-width: 36px; min-height: 36px; padding: 0; font-size: 15px; }

  #vocabTable tr.current-row td, #vocabTable tr.active-row td, #vocabTable tr.is-current td { background: transparent !important; border-color: transparent !important; }
  #vocabTable tbody tr.current-row, #vocabTable tbody tr.active-row, #vocabTable tbody tr.is-current {
    outline: 2px solid var(--accent-neon) !important;
    outline-offset: 0 !important;
    box-shadow: none !important;
    background: var(--bg-card) !important;
  }

  @supports (height: 100svh) {
    .workspace-layout { min-height: 100svh; }
  }
}

@media (max-width: 420px) {
  #status { display: none; }
  .toggle-mobile-menu-btn { min-width: 108px; font-size: 13px; }
  #vocabTable tbody tr { grid-template-columns: minmax(64px, .7fr) minmax(0, 1.2fr) 36px 36px; padding: 11px; }
  #vocabTable tbody td:nth-child(3) { font-size: 28px; }
  #vocabTable tbody td:nth-child(5) { font-size: 17px; }
}

/* Image modal */
.modal-image-overlay { position: fixed; right: 18px; bottom: calc(18px + env(safe-area-inset-bottom, 0px)); width: 300px; z-index: 9999; display: none; pointer-events: none; }
.modal-image-overlay.active { display: flex; }
.modal-image-card { width: 100%; display: flex; flex-direction: column; overflow: hidden; pointer-events: auto; background: var(--bg-card); border: 1px solid var(--border-color); border-radius: 18px; box-shadow: var(--shadow-popup); }
.modal-img-container { width: 100%; height: 180px; background: #02070d; display: flex; align-items: center; justify-content: center; position: relative; padding: 8px; }
.modal-img-container img { max-width: 100%; max-height: 100%; object-fit: contain; border-radius: 8px; }
.modal-close-btn { position: absolute; top: 9px; right: 9px; background: rgba(0,0,0,.62); border: 1px solid rgba(255,255,255,.18); color: #fff; width: 28px; height: 28px; border-radius: 50%; cursor: pointer; }
.modal-footer { padding: 11px 14px; border-top: 1px solid var(--border-subtle); }
.modal-status-bar { display: flex; justify-content: space-between; color: var(--text-secondary); font-size: 10px; margin-bottom: 6px; font-weight: 900; text-transform: uppercase; }
#modalCaptionText { font-size: 14px; font-weight: 850; color: var(--text-primary); }
@media (max-width: 768px) {
  .modal-image-overlay { right: 10px; left: 10px; bottom: calc(10px + env(safe-area-inset-bottom, 0px)); width: auto; }
  .modal-image-card { max-height: min(38dvh, 260px); }
  .modal-img-container { height: 130px; }
}

/* Speaking popup (legacy dictionary IDs/classes retained for JS compatibility) */
#dictionaryPopupOverlay, #dictionaryPopupOverlay * { box-sizing: border-box; }
#dictionaryPopupOverlay { position: fixed; inset: 0; z-index: 2147482500; display: none; align-items: center; justify-content: center; padding: 14px; pointer-events: none; background: rgba(0,0,0,.24); }
#dictionaryPopupOverlay.active { display: flex; }
#dictionaryPopupOverlay .dictionary-popup-card { width: min(820px, 62vw); min-width: 420px; height: min(760px, 92dvh); display: flex; flex-direction: column; overflow: hidden; pointer-events: auto; border-radius: 20px; border: 1px solid var(--border-color); background: var(--bg-card); color: var(--text-primary); box-shadow: var(--shadow-popup); }
.dictionary-popup-header { flex: 0 0 auto; min-height: 58px; padding: 10px 12px 10px 14px; display: flex; align-items: center; justify-content: space-between; gap: 10px; background: var(--bg-panel); border-bottom: 1px solid var(--border-subtle); }
.dictionary-popup-title-wrap { min-width: 0; display: flex; align-items: center; gap: 10px; }
.dictionary-popup-icon { flex: 0 0 auto; width: 34px; height: 34px; display: inline-flex; align-items: center; justify-content: center; border-radius: 12px; background: var(--accent-neon-dim); border: 1px solid var(--border-color); }
.dictionary-popup-title { font-size: 12px; color: var(--text-muted); font-weight: 900; text-transform: uppercase; letter-spacing: .05em; }
.dictionary-popup-word { max-width: 280px; margin-top: 3px; font-family: var(--font-hanzi); font-size: 20px; color: var(--text-primary); font-weight: 900; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.dictionary-popup-close { flex: 0 0 auto; width: 32px; height: 32px; display: inline-flex; align-items: center; justify-content: center; border: 1px solid var(--border-subtle); border-radius: 50%; background: var(--bg-input); color: var(--text-primary); cursor: pointer; }
.dictionary-popup-frame-wrap { flex: 1 1 auto; min-height: 0; background: #fff; }
.dictionary-popup-frame { display: block; width: 100%; height: 100%; border: 0; background: #fff; }
.dictionary-popup-footer { flex: 0 0 auto; min-height: 38px; padding: 8px 12px; display: flex; align-items: center; justify-content: space-between; gap: 10px; border-top: 1px solid var(--border-subtle); background: var(--bg-panel); color: var(--text-secondary); font-size: 11px; font-weight: 800; }
.dictionary-popup-footer a { color: var(--accent-neon); text-decoration: none; font-weight: 900; }
@media (max-width: 768px) {
  #dictionaryPopupOverlay { align-items: stretch; justify-content: stretch; padding: 0; background: rgba(0,0,0,.32); }
  #dictionaryPopupOverlay .dictionary-popup-card { width: 100vw; height: 100dvh; min-width: 0; min-height: 0; border-radius: 0; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation: none !important; transition: none !important; scroll-behavior: auto !important; }
}

/* ============================================================================
MOBILE TYPING SCROLL PATCH V2
============================================================================ */
@media (max-width: 768px) {
  .table-container {
    scroll-padding-top: 18px;
    scroll-padding-bottom: max(260px, 48dvh);
    padding-bottom: calc(max(260px, 48dvh) + env(safe-area-inset-bottom, 0px));
  }

  #vocabTable tbody::after {
    content: "";
    display: block;
    height: max(260px, 48dvh);
  }

  #vocabTable tbody tr {
    scroll-margin-top: 18px;
    scroll-margin-bottom: max(260px, 48dvh);
  }

  #vocabTable tbody tr.current-row,
  #vocabTable tbody tr.active-row,
  #vocabTable tbody tr.is-current {
    scroll-margin-top: 24px;
    scroll-margin-bottom: max(280px, 52dvh);
  }

  #vocabTable .answer-input {
    font-size: 16px;
    touch-action: manipulation;
  }
}

/* ============================================================================
EXPLORER KNOWLEDGE SHEET V2
============================================================================ */
.workspace-view-toggle {
  color: var(--accent-neon);
  border-color: rgba(24, 183, 255, .38);
  background: var(--accent-neon-dim);
}
.workspace-view-toggle:hover { color: #fff; background: var(--accent-neon); }
.view-explorer .sub-panel { display: none; }
.view-explorer .table-container { padding-top: 14px; }

.view-explorer #vocabTable {
  min-width: 1180px;
  table-layout: fixed;
  border-spacing: 0 10px;
}
.view-explorer #vocabTable th {
  padding: 10px 8px;
  background: var(--bg-main);
}
.view-explorer #vocabTable th:nth-child(1),
.view-explorer #vocabTable td:nth-child(1) { width: 58px; text-align: right; }
.view-explorer #vocabTable th:nth-child(2),
.view-explorer #vocabTable td:nth-child(2) { width: 62px; text-align: left; }
.view-explorer #vocabTable th:nth-child(3),
.view-explorer #vocabTable td:nth-child(3) { width: 180px; text-align: center; }
.view-explorer #vocabTable th:nth-child(4),
.view-explorer #vocabTable td:nth-child(4) { width: 230px; }
.view-explorer #vocabTable th:nth-child(5),
.view-explorer #vocabTable td:nth-child(5) { width: auto; }
.view-explorer #vocabTable th:nth-child(6),
.view-explorer #vocabTable td:nth-child(6) { width: 76px; text-align: center; }

.view-explorer #vocabTable tbody tr.explorer-row {
  content-visibility: auto;
  contain-intrinsic-size: auto 128px;
  cursor: default;
}
.view-explorer #vocabTable tbody tr.explorer-row > td {
  height: 128px;
  padding: 14px 12px;
  vertical-align: top;
  background: var(--bg-card);
  border-top: 1px solid var(--border-subtle);
  border-bottom: 1px solid var(--border-subtle);
}
.view-explorer #vocabTable tbody tr.explorer-row > td:first-child {
  border-left: 1px solid var(--border-subtle);
  border-radius: 16px 0 0 16px;
}
.view-explorer #vocabTable tbody tr.explorer-row > td:last-child {
  border-right: 1px solid var(--border-subtle);
  border-radius: 0 16px 16px 0;
}
.view-explorer #vocabTable tbody tr.explorer-row:hover > td { background: var(--bg-card-hover); }

/* FIX CHỌN THẺ TỪ VỰNG CHI TIẾT: Chỉ viền xanh, không đổi màu nền */
.view-explorer #vocabTable tbody tr.explorer-row.current-row > td {
  background: var(--bg-card) !important;
  border-top-color: var(--accent-neon) !important;
  border-bottom-color: var(--accent-neon) !important;
}
.view-explorer #vocabTable tbody tr.explorer-row.current-row > td:first-child { border-left-color: var(--accent-neon) !important; }
.view-explorer #vocabTable tbody tr.explorer-row.current-row > td:last-child { border-right-color: var(--accent-neon) !important; }
.view-explorer #vocabTable tbody tr.explorer-row.current-row {
  outline: 2px solid var(--accent-neon) !important;
  outline-offset: -2px;
  filter: none !important;
}
.view-explorer #vocabTable tbody tr.explorer-row.details-open > td {
  border-bottom-color: transparent;
  border-radius: 16px 16px 0 0;
}

.explorer-index-cell, .explorer-note-cell { vertical-align: top !important; padding-top: 20px !important; }
.explorer-index {
  display: inline-flex;
  min-width: 28px;
  justify-content: flex-end;
  color: var(--text-secondary);
  font-family: var(--font-mono);
  font-size: 15px;
  font-weight: 900;
}
.forgot-control {
  width: 34px;
  height: 34px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border: 1px solid var(--border-subtle);
  border-radius: 11px;
  background: var(--bg-input);
  cursor: pointer;
}
.forgot-control:hover { border-color: var(--accent-orange); background: var(--accent-orange-dim); }
.forgot-control .forgot-checkbox { width: 17px; height: 17px; accent-color: var(--accent-orange); cursor: pointer; }

.explorer-hanzi-cell { position: relative; vertical-align: top !important; }
.hanzi-tools {
  position: absolute;
  top: 8px;
  left: 8px;
  z-index: 2;
  min-height: 24px;
  display: flex;
  align-items: center;
  justify-content: flex-start;
  gap: 4px;
  margin: 0;
}
.compact-tool.speaker-icon, .compact-tool.dict-icon {
  width: 22px;
  height: 22px;
  min-width: 22px;
  min-height: 22px;
  padding: 0;
  border: 0;
  border-radius: 0;
  background: transparent;
  box-shadow: none;
  font-size: 13px;
  opacity: .82;
}
.compact-tool.speaker-icon:hover, .compact-tool.dict-icon:hover {
  color: var(--accent-neon);
  background: transparent;
  opacity: 1;
}
.hanzi-focus {
  display: block;
  width: 100%;
  padding: 30px 0 0;
  border: 0;
  background: transparent;
  color: var(--text-primary);
  font-family: var(--font-hanzi);
  font-size: clamp(38px, 3.2vw, 52px);
  font-weight: 900;
  line-height: 1.05;
  cursor: pointer;
}
.hanzi-focus:hover { color: var(--accent-neon); }

.explorer-vocab-cell {
  vertical-align: top !important;
  text-align: left !important;
}
.pos-badge {
  display: inline-flex;
  align-items: center;
  min-height: 27px;
  max-width: 100%;
  margin-bottom: 8px;
  padding: 5px 9px;
  border: 1px solid var(--border-color);
  border-radius: 999px;
  background: var(--accent-neon-dim);
  color: var(--accent-neon);
  font-size: 11px;
  font-weight: 900;
  line-height: 1.2;
}
.explorer-pinyin {
  display: block;
  margin-bottom: 5px;
  color: var(--accent-neon);
  font-family: var(--font-mono);
  font-size: 16px;
  line-height: 1.25;
}
.explorer-meaning {
  display: block;
  color: var(--accent-orange);
  font-size: 18px;
  font-weight: 900;
  line-height: 1.3;
  overflow-wrap: anywhere;
}

.explorer-memory-cell {
  display: grid;
  grid-template-columns: minmax(180px, .9fr) minmax(240px, 1.35fr);
  gap: 12px;
  align-items: start;
  align-content: start;
  text-align: left !important;
  vertical-align: top !important;
}
.knowledge-block {
  min-width: 0;
  padding: 11px 13px;
  border: 1px solid var(--border-subtle);
  border-radius: 12px;
  background: var(--bg-input);
}
.knowledge-label {
  display: block;
  margin-bottom: 6px;
  color: var(--text-muted);
  font-size: 10px;
  font-weight: 900;
  letter-spacing: .08em;
  text-transform: uppercase;
}
.knowledge-block p {
  margin: 0;
  color: var(--accent-orange);
  font-size: 13.5px;
  font-weight: 700;
  line-height: 1.45;
  overflow-wrap: anywhere;
}
.mnemonic-block { background: linear-gradient(135deg, var(--accent-orange-dim), var(--bg-input)); }
.mnemonic-block p {
  display: -webkit-box;
  overflow: hidden;
  -webkit-box-orient: vertical;
  -webkit-line-clamp: 3;
}

.explorer-detail-cell { vertical-align: top !important; padding-top: 18px !important; }

#vocabTable th:nth-child(6),
#vocabTable td:nth-child(6),
.view-explorer #vocabTable th:nth-child(6),
.view-explorer #vocabTable td:nth-child(6),
#vocabTable td.explorer-detail-cell {
  width: 84px !important;
  max-width: 84px !important;
  min-width: 84px !important;
  white-space: nowrap;
}
.explorer-detail-cell { text-align: center !important; }
.detail-toggle {
  width: 62px;
  min-height: 58px;
  display: inline-flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 4px;
  padding: 7px 5px;
  border: 1px solid var(--border-color);
  border-radius: 13px;
  background: var(--bg-input);
  color: var(--text-secondary);
  font: 900 12px/1 var(--font-ui);
  cursor: pointer;
}
.detail-toggle:hover, .detail-toggle[aria-expanded="true"] {
  color: var(--accent-neon);
  border-color: var(--accent-neon);
  background: var(--accent-neon-dim);
}
.detail-toggle-icon { font-size: 18px; line-height: 1; }

.explorer-detail-row[hidden] { display: none !important; }
#vocabTable .explorer-detail-row td {
  padding: 0 12px 14px;
  border: 0;
  border-radius: 0 0 16px 16px;
  background: var(--bg-card);
}
.explorer-detail-card {
  display: grid;
  grid-template-columns: minmax(320px, .85fr) minmax(480px, 1.35fr);
  gap: 14px;
  padding: 15px;
  border: 1px solid var(--border-color);
  border-radius: 14px;
  background: var(--bg-panel);
}
.detail-section { min-width: 0; }
.detail-section h4 {
  margin: 0 0 9px;
  color: var(--text-muted);
  font-size: 11px;
  font-weight: 900;
  letter-spacing: .08em;
  text-transform: uppercase;
}
.detail-section p { margin: 0; color: var(--text-secondary); line-height: 1.55; }
.detail-items { display: grid; gap: 8px; }

/* FIX CÂU VÍ DỤ / TỪ GHÉP: Xếp dòng dọc từ trên xuống dưới */
.detail-item {
  display: flex !important;
  flex-direction: column !important;
  align-items: flex-start !important;
  gap: 6px !important;
  width: 100% !important;
  padding: 12px 14px !important;
  border: 1px solid var(--border-subtle);
  border-radius: 11px;
  background: var(--bg-input);
}
.detail-zh { color: var(--text-primary); font-family: var(--font-hanzi); font-size: 18px; }
.detail-pinyin { color: var(--accent-neon); font-family: var(--font-mono); font-size: 12px; }
.detail-vi {
  color: var(--accent-orange);
  font-size: 13px;
  font-weight: 700;
  width: 100% !important;
  white-space: normal !important;
  word-break: break-word !important;
}
.detail-section.is-empty { opacity: .65; }
.empty-row td { text-align: center !important; color: var(--text-muted); padding: 40px !important; font-weight: 800; }
tr.forgotten-row .forgot-control { border-color: var(--accent-orange); background: var(--accent-orange-dim); }

@media (max-width: 1100px) and (min-width: 769px) {
  .view-explorer #vocabTable { min-width: 1040px; }
  .view-explorer #vocabTable th:nth-child(3), .view-explorer #vocabTable td:nth-child(3) { width: 150px; }
  .view-explorer #vocabTable th:nth-child(4), .view-explorer #vocabTable td:nth-child(4) { width: 205px; }
  .explorer-memory-cell { grid-template-columns: 1fr; gap: 8px; }
  .mnemonic-block p { -webkit-line-clamp: 2; }
}

/* ========================================================================== 
   MOBILE UI REFINEMENT V3 & V4
   ========================================================================== */
@media (max-width: 768px) {
  :root { --nav-h: 60px; }

  .top-navbar {
    min-height: var(--nav-h);
    padding: 8px 10px;
    gap: 7px;
  }
  .top-navbar > div:first-child {
    display: flex;
    align-items: center;
    flex: 1 1 auto;
    min-width: 0;
    gap: 7px !important;
  }
  #backToDashBtn {
    flex: 0 0 42px;
    width: 42px;
    min-width: 42px;
    height: 42px;
    padding: 0;
    font-size: 0;
  }
  #backToDashBtn::before {
    content: "←";
    font-size: 20px;
    line-height: 1;
  }
  .workspace-view-toggle {
    flex: 0 0 auto;
    min-width: 84px;
    height: 42px;
    padding: 0 11px;
    font-size: 12px;
  }
  #status {
    flex: 1 1 auto;
    min-width: 0;
    max-width: none;
    font-size: 11px !important;
  }
  .nav-controls-wrapper { gap: 0; }
  .toggle-mobile-menu-btn {
    min-width: 46px;
    width: 46px;
    height: 42px;
    padding: 0;
    font-size: 0;
  }
  .toggle-mobile-menu-btn::before {
    content: "⚙";
    font-size: 18px;
  }
  .toggle-mobile-menu-btn .arrow-icon {
    display: inline-block;
    margin-left: 2px;
    font-size: 10px;
  }

  .table-container,
  .view-explorer .table-container {
    padding: 8px 8px calc(16px + env(safe-area-inset-bottom, 0px));
    overflow-x: hidden;
    scroll-padding-top: 8px;
    scrollbar-gutter: auto;
  }

  /* Grid Layout Explorer Mode */
  .view-explorer #vocabTable,
  .view-explorer #vocabTable tbody {
    display: block;
    width: 100%;
    min-width: 0;
  }
  
  .view-explorer #vocabTable tbody tr.explorer-row {
    display: grid;
    grid-template-columns: 32px 36px minmax(0, 1fr) 42px;
    grid-template-areas:
      "stt note vocab detail"
      "hanzi hanzi vocab detail"
      "memory memory memory memory";
    column-gap: 8px;
    row-gap: 10px;
    width: 100%;
    margin: 0 0 9px;
    padding: 11px 11px 14px;
    border-radius: 16px;
    contain-intrinsic-size: auto 300px;
    overflow: hidden;
  }
  
  .view-explorer #vocabTable tbody tr.explorer-row > td {
    width: auto !important;
    min-width: 0 !important;
    max-width: none !important;
    height: auto !important;
    padding: 0 !important;
    white-space: normal;
  }

  .view-explorer #vocabTable tbody tr.explorer-row > td:nth-child(1) {
    grid-area: stt;
    display: flex;
    justify-content: flex-start !important;
    align-items: center !important;
  }
  .view-explorer #vocabTable tbody tr.explorer-row > td:nth-child(2) {
    grid-area: note;
    display: flex;
    justify-content: flex-start !important;
    align-items: center !important;
  }
  .view-explorer #vocabTable tbody tr.explorer-row > td:nth-child(3) {
    grid-area: hanzi;
    position: relative;
    display: flex !important;
    min-height: 92px !important;
    align-items: center !important;
    justify-content: center !important;
    overflow: visible !important;
  }
  .view-explorer #vocabTable tbody tr.explorer-row > td:nth-child(4) {
    grid-area: vocab;
    display: flex;
    flex-direction: column;
    align-self: center;
    justify-content: center !important;
    padding-left: 2px !important;
  }
  .view-explorer #vocabTable tbody tr.explorer-row > td:nth-child(5) {
    grid-area: memory;
    display: grid !important;
    grid-template-columns: 1fr;
    gap: 7px;
    padding-top: 2px !important;
  }
  .view-explorer #vocabTable tbody tr.explorer-row > td:nth-child(6) {
    grid-area: detail;
    display: flex;
    align-self: center; 
    justify-self: end;
    align-items: center !important;
    justify-content: center !important;
  }

  .view-explorer #vocabTable tbody tr.explorer-row > td::before {
    display: none !important;
  }

  .explorer-index {
    min-width: 0;
    justify-content: flex-start;
    font-size: 12px;
    color: var(--text-muted);
  }
  .explorer-index::before { content: "#"; margin-right: 1px; }
  .explorer-note-cell { margin-left: 0 !important; }
  
  .forgot-control {
    width: 28px;
    height: 28px;
    border-radius: 8px;
  }
  .forgot-control .forgot-checkbox { width: 15px; height: 15px; }

  /* Tối ưu các nút âm thanh & từ điển */
  .hanzi-tools { top: -2px; left: -2px; gap: 8px; }
  .compact-tool.speaker-icon,
  .compact-tool.dict-icon {
    width: 28px;
    height: 28px;
    min-width: 28px;
    min-height: 28px;
    border: 0;
    border-radius: 6px;
    background: rgba(255, 255, 255, 0.05);
    font-size: 13px;
  }
  .hanzi-focus {
    display: block;
    width: 100%;
    padding: 26px 2px 2px;
    text-align: center;
    font-size: clamp(38px, 12vw, 50px);
    line-height: 1.05;
  }

  .pos-badge {
    align-self: flex-start;
    min-height: 22px;
    margin-bottom: 5px;
    padding: 3px 7px;
    font-size: 10px;
  }
  .explorer-pinyin { margin-bottom: 4px; font-size: 15px; line-height: 1.25; }
  .explorer-meaning { font-size: 17px; line-height: 1.3; }

  .knowledge-block { padding: 9px 10px; border-radius: 11px; }
  .knowledge-label { margin-bottom: 4px; font-size: 9px; }
  .knowledge-block p { font-size: 13px; line-height: 1.42; }
  .mnemonic-block p { display: block; overflow: visible; -webkit-line-clamp: unset; }

  /* Nút thu gọn/mở rộng gọn gàng */
  .detail-toggle {
    width: 40px;
    min-width: 40px;
    height: 54px; 
    padding: 0;
    border-radius: 12px;
  }
  .detail-toggle-icon { font-size: 20px; }

  /* Sửa phần bảng mở rộng (Từ ghép & Ví dụ) */
  #vocabTable tbody tr.explorer-detail-row:not([hidden]) {
    display: block;
    width: 100%;
    margin: -3px 0 9px;
  }
  #vocabTable tbody tr.explorer-detail-row[hidden] { display: none !important; }
  
  #vocabTable .explorer-detail-row td {
    display: block;
    width: 100% !important;
    min-width: 0 !important;
    max-width: none !important;
  }

  /* Ẩn chữ STT ở khối chi tiết */
  #vocabTable .explorer-detail-row td::before {
    display: none !important;
    content: "" !important;
  }

  .explorer-detail-card { 
    grid-template-columns: 1fr; 
    gap: 12px; 
    padding: 12px; 
    border-radius: 14px; 
  }
  .detail-items { gap: 8px; }

  /* Mode review mặc định */
  #vocabTable tbody tr:not(.explorer-row):not(.explorer-detail-row) {
    grid-template-columns: minmax(78px, .8fr) minmax(0, 1.2fr) 40px 40px;
    row-gap: 8px;
    padding: 11px;
    border-radius: 16px;
  }
  
  .view-review #vocabTable .answer-input { width: 100%; }
  .view-review #vocabTable .speaker-icon,
  .view-review #vocabTable .dict-icon {
    width: 38px;
    height: 38px;
    min-width: 38px;
    min-height: 38px;
  }

  .mobile-collapse-menu {
    left: 8px;
    right: 8px;
    top: calc(var(--nav-h) + 6px);
    gap: 8px;
    padding: 9px;
    border-radius: 16px;
  }
}

/* ============================================================================
IDIOM EXPLORER LAYOUT V2 (DESKTOP & MOBILE FIX HOÀN CHỈNH)
============================================================================ */
.badge-line { display: flex; align-items: center; flex-wrap: wrap; gap: 6px; margin-bottom: 7px; }
.badge-line .pos-badge { margin: 0; }
.frequency-badge { display: inline-flex; align-items: center; min-height: 27px; padding: 5px 9px; border: 1px solid rgba(52,211,153,.3); border-radius: 999px; background: var(--accent-green-dim); color: var(--accent-green); font-size: 10px; font-weight: 900; }

/* Cố định layout bằng class .is-idiom-mode do JS cấp */
.view-explorer #vocabTable.is-idiom-mode {
  min-width: 1480px !important;
  table-layout: fixed !important;
}

.view-explorer #vocabTable.is-idiom-mode .idiom-table-head th:nth-child(1),
.view-explorer #vocabTable.is-idiom-mode tbody tr.idiom-row > td:nth-child(1) { width: 58px !important; text-align: center; }

.view-explorer #vocabTable.is-idiom-mode .idiom-table-head th:nth-child(2),
.view-explorer #vocabTable.is-idiom-mode tbody tr.idiom-row > td:nth-child(2) { width: 62px !important; text-align: center; }

.view-explorer #vocabTable.is-idiom-mode .idiom-table-head th:nth-child(3),
.view-explorer #vocabTable.is-idiom-mode tbody tr.idiom-row > td:nth-child(3) { width: 210px !important; }

.view-explorer #vocabTable.is-idiom-mode .idiom-table-head th:nth-child(4),
.view-explorer #vocabTable.is-idiom-mode tbody tr.idiom-row > td:nth-child(4) { width: 360px !important; }

.view-explorer #vocabTable.is-idiom-mode .idiom-table-head th:nth-child(5),
.view-explorer #vocabTable.is-idiom-mode tbody tr.idiom-row > td:nth-child(5) { width: 285px !important; }

.view-explorer #vocabTable.is-idiom-mode .idiom-table-head th:nth-child(6),
.view-explorer #vocabTable.is-idiom-mode tbody tr.idiom-row > td:nth-child(6) { width: auto !important; min-width: 360px !important; text-align: left; }

.view-explorer #vocabTable.is-idiom-mode .idiom-table-head th:nth-child(7),
.view-explorer #vocabTable.is-idiom-mode tbody tr.idiom-row > td:nth-child(7) { width: 84px !important; text-align: center; }

.view-explorer #vocabTable tbody tr.idiom-row > td {
  height: 190px;
  padding: 14px 13px;
  vertical-align: top;
  background: var(--bg-card);
  border-top: 1px solid var(--border-subtle);
  border-bottom: 1px solid var(--border-subtle);
}
.view-explorer #vocabTable tbody tr.idiom-row > td:first-child { border-left: 1px solid var(--border-subtle); border-radius: 16px 0 0 16px; }
.view-explorer #vocabTable tbody tr.idiom-row > td:last-child { border-right: 1px solid var(--border-subtle); border-radius: 0 16px 16px 0; }

/* FIX CHỌN THỂ THÀNH NGỮ DESKTOP: Chỉ hiện viền xanh neon, KHÔNG đổi màu nền */
.view-explorer #vocabTable tbody tr.idiom-row.current-row > td {
  background: var(--bg-card) !important;
  border-top-color: var(--accent-neon) !important;
  border-bottom-color: var(--accent-neon) !important;
}
.view-explorer #vocabTable tbody tr.idiom-row.current-row > td:first-child { border-left-color: var(--accent-neon) !important; }
.view-explorer #vocabTable tbody tr.idiom-row.current-row > td:last-child { border-right-color: var(--accent-neon) !important; }

.idiom-hanzi-cell { position: relative; }
.idiom-row .hanzi-focus { padding: 34px 4px 0; font-size: clamp(34px,2.7vw,48px); line-height: 1.15; overflow-wrap: anywhere; }
.idiom-meaning-cell, .idiom-memory-cell, .idiom-allusion-cell { text-align: left !important; }
.idiom-meaning-cell .explorer-pinyin { margin-bottom: 9px; }
.idiom-meaning-pair { display: grid; gap: 7px; }
.idiom-meaning-pair > div, .idiom-memory-cell, .idiom-allusion-cell { padding: 10px 12px; border: 1px solid var(--border-subtle); border-radius: 12px; background: var(--bg-input); }

/* Khóa chống tràn văn bản */
.idiom-meaning-pair p, .idiom-memory-cell p, .idiom-allusion-cell p {
  margin: 0;
  color: var(--accent-orange);
  font-size: 13px;
  font-weight: 750;
  line-height: 1.48;
  overflow-wrap: anywhere;
  word-break: break-word;
}
.idiom-meaning-pair > div:nth-child(2) p { color: var(--text-primary); }
.idiom-memory-cell { background: linear-gradient(135deg, var(--accent-green-dim), var(--bg-input)); }
.idiom-memory-cell p { color: var(--accent-green); }
.idiom-allusion-cell p { color: var(--text-secondary); display: -webkit-box; -webkit-box-orient: vertical; -webkit-line-clamp: 7; overflow: hidden; }
.idiom-row .explorer-detail-cell { padding-top: 65px !important; }

#vocabTable .idiom-detail-row td { padding: 0 12px 16px; border: 0; border-radius: 0 0 16px 16px; background: var(--bg-card); }
.idiom-detail-two-columns { display: grid; grid-template-columns: minmax(360px,.85fr) minmax(560px,1.35fr); gap: 14px; padding: 15px; border: 1px solid var(--border-color); border-radius: 14px; background: var(--bg-panel); }
.idiom-detail-column { display: grid; align-content: start; gap: 14px; min-width: 0; }
.idiom-detail-column > .detail-section, .idiom-detail-column > .idiom-text-section { padding: 12px 14px; border: 1px solid var(--border-subtle); border-radius: 12px; background: var(--bg-input); }
.idiom-detail-column .detail-item { grid-template-columns: minmax(110px,auto) minmax(130px,auto) 1fr auto; }
.relation-type { display: inline-flex; padding: 3px 7px; border-radius: 999px; background: var(--accent-neon-dim); color: var(--accent-neon); font-size: 10px; font-weight: 900; }
.equivalent-section p { color: var(--accent-orange); font-weight: 800; }

/* FIX CĂN TRÁI TẤT CẢ TIÊU ĐỀ TRONG XEM CHI TIẾT */
.detail-section h4,
.idiom-text-section h4,
.detail-section,
.idiom-text-section {
  text-align: left !important;
}

@media (max-width: 1100px) and (min-width: 769px) {
  .view-explorer #vocabTable.is-idiom-mode { min-width: 1320px !important; }
  .idiom-detail-two-columns { grid-template-columns: minmax(320px,.8fr) minmax(500px,1.2fr); }
}

/* ============================================================================
FIX HOÀN TOÀN DIỆN THOẠI CHO THÀNH NGỮ
============================================================================ */
@media (max-width: 768px) {
  .view-explorer #vocabTable.is-idiom-mode {
    min-width: 0 !important;
    width: 100% !important;
  }

  .view-explorer #vocabTable.is-idiom-mode tbody tr.idiom-row {
    display: flex !important;
    flex-direction: column !important;
    gap: 12px !important;
    width: 100% !important;
    height: auto !important;
    min-height: 0 !important;
    padding: 14px !important;
    margin: 0 0 12px 0 !important;
    border: 1px solid var(--border-color) !important;
    border-radius: 16px !important;
    background: var(--bg-card) !important;
    position: relative !important;
    contain-intrinsic-size: auto !important;
  }

  /* FIX CHỌN THẺ THÀNH NGỮ MOBILE: Chỉ nổi viền xanh neon */
  .view-explorer #vocabTable.is-idiom-mode tbody tr.idiom-row.current-row {
    outline: 2px solid var(--accent-neon) !important;
    border-color: var(--accent-neon) !important;
    background: var(--bg-card) !important;
    box-shadow: none !important;
  }

  /* Reset toàn bộ ô td thành khối linh hoạt, không position absolute */
  .view-explorer #vocabTable.is-idiom-mode tbody tr.idiom-row > td {
    display: block !important;
    width: 100% !important;
    height: auto !important;
    min-height: 0 !important;
    padding: 0 !important;
    border: none !important;
    background: transparent !important;
    position: static !important;
  }

  .idiom-row .explorer-index-cell {
    display: flex !important;
    align-items: center !important;
    justify-content: space-between !important;
    font-size: 13px !important;
    color: var(--text-muted) !important;
  }

  .idiom-row .idiom-hanzi-cell {
    position: relative !important;
    text-align: center !important;
    padding: 6px 0 !important;
  }

  .idiom-row .hanzi-tools {
    position: static !important;
    display: flex !important;
    justify-content: center !important;
    gap: 10px !important;
    margin-bottom: 4px !important;
  }

  .idiom-row .hanzi-focus {
    padding: 0 !important;
    font-size: clamp(32px, 9vw, 44px) !important;
    line-height: 1.2 !important;
    text-align: center !important;
    width: 100% !important;
  }

  .idiom-row .idiom-meaning-cell,
  .idiom-row .idiom-memory-cell,
  .idiom-row .idiom-allusion-cell {
    width: 100% !important;
    text-align: left !important;
  }

  .idiom-meaning-pair {
    grid-template-columns: 1fr !important;
    gap: 8px !important;
  }

  .idiom-allusion-cell p {
    -webkit-line-clamp: unset !important;
    display: block !important;
  }

  .idiom-row .explorer-detail-cell {
    padding-top: 4px !important;
    display: flex !important;
    justify-content: center !important;
  }

  .idiom-row .detail-toggle {
    width: 100% !important;
    height: 40px !important;
    min-height: 40px !important;
    flex-direction: row !important;
    gap: 6px !important;
    border-radius: 10px !important;
  }

  .idiom-detail-two-columns {
    grid-template-columns: 1fr !important;
    padding: 11px !important;
    gap: 11px !important;
  }

  .idiom-detail-column .detail-item {
    grid-template-columns: 1fr !important;
    gap: 4px !important;
  }

  #vocabTable .idiom-detail-row td {
    display: block !important;
    width: 100% !important;
    padding: 0 0 12px !important;
  }

  #vocabTable .idiom-detail-row td::before {
    display: none !important;
  }
}
/* ============================================================================
ĐỒNG BỘ ĐIỂN TÍCH GIỐNG HỆT NGHĨA ĐEN
============================================================================ */
.idiom-allusion-cell p,
.allusion-section p {
  color: var(--accent-orange) !important; /* Màu vàng cam giống Nghĩa đen */
  font-size: 13px !important;              /* Cỡ chữ chuẩn Nghĩa đen */
  font-weight: 750 !important;             /* Độ đậm chuẩn Nghĩa đen */
  line-height: 1.48 !important;            /* Giãn dòng chuẩn */
  display: block !important;
  -webkit-line-clamp: unset !important;    /* Bỏ giới hạn cắt dòng */
  overflow: visible !important;
  white-space: normal !important;           /* Xuống dòng tự nhiên */
  word-break: break-word !important;        /* Tự ngắt từ tránh tràn viền */
  overflow-wrap: anywhere !important;
}

/* Đảm bảo ô Điển tích giãn chiều cao tự do theo câu chữ */
.view-explorer #vocabTable tbody tr.idiom-row > td.idiom-allusion-cell,
.view-explorer #vocabTable tbody tr.idiom-row > td {
  height: auto !important;
  min-height: 190px;
}

/* ============================================================================
SPEAKING POPUP BRANDING
Giữ selector dictionary-popup cũ để tương thích matrix-workspace.js,
nhưng toàn bộ trải nghiệm hiển thị là Speaking Mode của Matrix Hanzi.
============================================================================ */
.speaking-popup-overlay .dictionary-popup-icon {
  color: var(--accent-green);
  background: var(--accent-green-dim);
  border-color: rgba(52, 211, 153, .30);
  font-size: 17px;
}

.speaking-popup-overlay .dictionary-popup-title {
  color: var(--accent-green);
  letter-spacing: .08em;
}

.speaking-popup-overlay .dictionary-popup-word {
  max-width: min(420px, 56vw);
}

.speaking-popup-overlay .dictionary-popup-frame-wrap,
.speaking-popup-overlay .dictionary-popup-frame {
  background: var(--bg-layer);
}

.speaking-popup-overlay .dictionary-popup-footer a {
  color: var(--accent-green);
}

/* Nút microphone dùng lại class dict-icon trong giai đoạn MVP. */
.dict-icon {
  color: var(--accent-green);
}

.dict-icon:hover,
.dict-icon:focus-visible {
  color: #fff;
  border-color: var(--accent-green);
  background: var(--accent-green);
  outline: none;
}

.compact-tool.dict-icon:hover,
.compact-tool.dict-icon:focus-visible {
  color: var(--accent-green);
  background: transparent;
}

@media (max-width: 768px) {
  .speaking-popup-overlay .dictionary-popup-header {
    min-height: 60px;
    padding-left: 12px;
  }

  .speaking-popup-overlay .dictionary-popup-word {
    max-width: 58vw;
  }

  .speaking-popup-overlay .dictionary-popup-footer {
    min-height: 42px;
    padding-bottom: max(8px, env(safe-area-inset-bottom, 0px));
  }
}

/* ============================================================================
SPEAKING POPUP BRANDING
Legacy dictionary-popup selectors remain so matrix-workspace.js keeps working.
============================================================================ */
.speaking-popup-overlay .dictionary-popup-icon {
  color: var(--accent-green);
  background: var(--accent-green-dim);
  border-color: rgba(52, 211, 153, .30);
  font-size: 17px;
}
.speaking-popup-overlay .dictionary-popup-title { color: var(--accent-green); letter-spacing: .08em; }
.speaking-popup-overlay .dictionary-popup-word { max-width: min(420px, 56vw); }
.speaking-popup-overlay .dictionary-popup-frame-wrap,
.speaking-popup-overlay .dictionary-popup-frame { background: var(--bg-layer); }
.speaking-popup-overlay .dictionary-popup-footer a { color: var(--accent-green); }
.dict-icon { color: var(--accent-green); }
.dict-icon:hover, .dict-icon:focus-visible {
  color: #fff; border-color: var(--accent-green); background: var(--accent-green); outline: none;
}
.compact-tool.dict-icon:hover, .compact-tool.dict-icon:focus-visible {
  color: var(--accent-green); background: transparent;
}
@media (max-width: 768px) {
  .speaking-popup-overlay .dictionary-popup-header { min-height: 60px; padding-left: 12px; }
  .speaking-popup-overlay .dictionary-popup-word { max-width: 58vw; }
  .speaking-popup-overlay .dictionary-popup-footer {
    min-height: 42px; padding-bottom: max(8px, env(safe-area-inset-bottom, 0px));
  }
}

/* ============================================================================
PAGE SPLIT: ON TAP
review.html is the Recall/Typing page.
============================================================================ */
body[data-workspace-page="review"] .workspace-view-toggle {
  text-decoration: none;
}
body[data-workspace-page="review"] .sub-panel {
  display: flex;
}
body[data-workspace-page="review"] #vocabTable {
  min-width: 1120px;
  table-layout: fixed;
}
@media (max-width: 768px) {
  body[data-workspace-page="review"] .workspace-layout > .sub-panel {
    display: none;
  }
  body[data-workspace-page="review"] #vocabTable {
    min-width: 0;
  }
}
