/* ═══════════════════════════════════════════════════
       CODE-SWITCH READER — Restyled in warm ink & paper
    ═══════════════════════════════════════════════════ */
    @import url('https://fonts.googleapis.com/css2?family=Playfair+Display:ital,wght@0,400;0,600;0,700;1,400&family=Lora:ital,wght@0,400;0,500;0,600;1,400&display=swap');

    :root {
      /* Light (parchment) palette */
      --bg-color:        #f5f0e8;
      --bg-gradient:     #f5f0e8;
      --card-bg:         #faf7f2;
      --text-color:      #1a1208;
      --muted:           #7a6e5f;
      --border-color:    rgba(26,18,8,0.13);
      --highlight-sent:  rgba(176, 90, 47, 0.13);
      --highlight-word:  #f5d08a;
      --word-text:       #1a1208;
      --accent:          #b05a2f;
      --accent-b:        #2f6eb0;
      --danger:          #9b2c2c;
      --success:         #2d6a4f;
      --paper-dark:      #ede7d9;
    }

    [data-theme="dark"] {
      --bg-color:        #1a1510;
      --bg-gradient:     #1a1510;
      --card-bg:         #221e18;
      --text-color:      #e8e0d0;
      --muted:           #8a7e6e;
      --border-color:    rgba(232,224,208,0.12);
      --highlight-sent:  rgba(201,122,74,0.2);
      --highlight-word:  #7a5a20;
      --word-text:       #f5f0e8;
      --accent:          #c97a4a;
      --accent-b:        #5a9fd4;
      --danger:          #e57373;
      --success:         #81c784;
      --paper-dark:      #2a2318;
    }

    * { box-sizing: border-box; }

    body {
      font-family: 'Lora', Georgia, serif;
      background: var(--bg-gradient);
      color: var(--text-color);
      padding: 28px 24px;
      padding-bottom: 130px;
      max-width: 920px;
      margin: 0 auto;
      transition: background 0.3s, color 0.3s;
      min-height: 100vh;
    }

    /* ── HEADER ── */
    .header {
      display: flex;
      justify-content: space-between;
      align-items: baseline;
      margin-bottom: 28px;
      padding-bottom: 16px;
      border-bottom: 1px solid var(--border-color);
    }
    h1 {
      margin: 0;
      font-family: 'Playfair Display', Georgia, serif;
      font-size: 1.6rem;
      font-weight: 600;
      letter-spacing: -0.02em;
      color: var(--text-color);
    }

    .theme-toggle {
      background: transparent;
      border: 1px solid var(--border-color);
      color: var(--text-color);
      padding: 6px 14px;
      border-radius: 40px;
      cursor: pointer;
      font-family: 'Lora', serif;
      font-size: 0.8rem;
      transition: all 0.18s;
      letter-spacing: 0.04em;
    }
    .theme-toggle:hover { background: var(--text-color); color: var(--bg-color); }

    /* ── CARDS ── */
    .card {
      background: var(--card-bg);
      padding: 22px;
      border-radius: 8px;
      box-shadow: 0 2px 12px rgba(26,18,8,0.06);
      margin-bottom: 16px;
      border: 1px solid var(--border-color);
      transition: background 0.3s;
    }

    /* ── VOICE GROUPS ── */
    .voice-group {
      margin-bottom: 18px;
      padding-bottom: 18px;
      border-bottom: 1px dashed var(--border-color);
    }
    .voice-group:last-child { border-bottom: none; margin-bottom: 0; padding-bottom: 0; }

    label {
      font-family: 'Lora', serif;
      font-weight: 600;
      display: block;
      margin-top: 5px;
      font-size: 0.85em;
      color: var(--muted);
      letter-spacing: 0.02em;
    }

    select {
      width: 100%;
      padding: 9px 12px;
      border-radius: 6px;
      border: 1px solid var(--border-color);
      margin-top: 5px;
      background: var(--bg-color);
      color: var(--text-color);
      font-family: 'Lora', serif;
      font-size: 0.88rem;
      outline: none;
      transition: border-color 0.2s;
    }
    select:focus { border-color: var(--accent); }

    .mini-speed-row {
      display: flex;
      align-items: center;
      gap: 10px;
      margin-top: 8px;
      font-size: 0.78em;
      color: var(--muted);
      font-family: 'Lora', serif;
      font-style: italic;
    }
    input[type="range"] { flex: 1; accent-color: var(--accent); cursor: pointer; }

    /* ── TEXT INPUTS ── */
    #textInput {
      width: 100%;
      height: 310px;
      padding: 18px;
      border-radius: 6px;
      border: 1px dashed var(--border-color);
      font-size: 0.95rem;
      line-height: 1.75;
      box-sizing: border-box;
      resize: vertical;
      font-family: 'Lora', serif;
      background: var(--bg-color);
      color: var(--text-color);
      outline: none;
      transition: border-color 0.2s;
    }
    #textInput:focus { border-color: var(--accent); border-style: solid; }
    #textInput::placeholder { color: var(--muted); font-style: italic; }

    /* ── READER DISPLAY ── */
    #readerDisplay {
      display: none;
      background: var(--bg-color);
      padding: 22px;
      border-radius: 6px;
      border: 1px solid var(--border-color);
      height: 310px;
      font-size: 1rem;
      line-height: 1.4;
      overflow-y: auto;
      border-left: 4px solid var(--accent);
      white-space: pre-wrap;
      resize: vertical;
      margin-bottom: 20px;
      content-visibility: auto;
      contain-intrinsic-size: 50px;
      font-family: 'Lora', serif;
      
      /* 2-Column Notebook Layout */
      column-count: 2;
      column-gap: 40px;
      column-rule: 1px dashed var(--border-color);
    }
    
    @media (max-width: 768px) {
      #readerDisplay, body.zen-mode #readerDisplay {
        column-count: 1;
        column-rule: none;
      }
    }

    .speedometer-icon {
      display: inline-block;
      width: 1.2em;
      height: 1.2em;
      vertical-align: text-bottom;
      margin-right: 4px;
      background-image: url('speedometer.svg');
      background-size: contain;
      background-repeat: no-repeat;
      background-position: center;
    }
    [data-theme="dark"] .speedometer-icon { filter: invert(1) brightness(0.9); }

    /* ── SILENT MODE ── */
    .silent-mode {
      opacity: 0.38;
      font-style: italic;
    }

    /* ── SENTENCE CONTAINERS ── */
    .sentence-container {
      display: block;
      break-inside: avoid;
      page-break-inside: avoid;
      margin-bottom: -20px;
      padding: 1px 6px;
      border-radius: 3px;
      transition: background 0.2s;
      white-space: pre-wrap;
      word-break: break-word;
      overflow-wrap: break-word;
    }

    /* Track 0 (main text) subtle dimming */
    .sentence-container[data-track="0"] {
      color: var(--muted);
      border-radius: 3px;
      padding: 1px 3px;
    }
    [data-theme="dark"] .sentence-container[data-track="0"] {
      color: var(--muted);
    }

    /* ── PLAY CUE ── */
    .play-cue {
      user-select: none;
      cursor: pointer;
      color: var(--accent);
      margin-right: 6px;
      font-size: 0.75em;
      opacity: 0.45;
      transition: opacity 0.2s;
    }
    .play-cue:hover { opacity: 1; }

    /* ── WORD SPANS ── */
    .word { cursor: pointer; border-radius: 2px; padding: 0 2px; font-family: 'Lora', serif; transition: background-color 0.08s ease-in-out, color 0.08s ease-in-out; }
    .word:hover { background-color: rgba(176,90,47,0.1); text-decoration: underline; text-decoration-color: var(--accent); }

    .active-sentence {
      background-color: var(--highlight-sent) !important;
      color: var(--text-color) !important;
      border-radius: 3px;
      position: relative;
      z-index: 10;
    }
    .active-word {
      background-color: var(--highlight-word) !important;
      color: var(--word-text) !important;
      border-radius: 2px;
    }
    .saved-flash { background-color: var(--success) !important; color: #fff !important; }

    /* ── BOTTOM BUTTON BAR ── */
    .btns {
      display: flex;
      gap: 6px;
      position: sticky;
      bottom: 0;
      background: var(--card-bg);
      padding: 14px 22px 18px;
      margin: 0 -22px -22px -22px;
      border-radius: 0 0 8px 8px;
      border-top: 1px solid var(--border-color);
      z-index: 100;
      box-shadow: 0 -4px 16px rgba(26,18,8,0.05);
      flex-wrap: nowrap;
      overflow-x: auto;
    }

    button.action-btn {
      flex: 1;
      border: none;
      border-radius: 40px;
      padding: 12px 10px;
      display: flex;
      justify-content: center;
      align-items: center;
      font-size: 0.88rem;
      font-weight: 600;
      font-family: 'Lora', serif;
      cursor: pointer;
      color: #fff;
      transition: transform 0.1s, opacity 0.2s, filter 0.2s;
      white-space: nowrap;
      letter-spacing: 0.02em;
    }
    button.action-btn:active { transform: scale(0.97); }
    button.action-btn:disabled { opacity: 0.45; cursor: not-allowed; }
    .play  { background: var(--success); }
    .pause { background: var(--accent); color: #fff !important; }
    .resume { background: var(--accent-b); }
    .stop  { background: var(--danger); flex: 2 !important; }

    .mini-btn {
      background: var(--bg-color);
      border: 1px solid var(--border-color);
      color: var(--text-color);
      width: 48px;
      border-radius: 40px;
      font-size: 18px;
      cursor: pointer;
      display: flex;
      align-items: center;
      justify-content: center;
      user-select: none;
      transition: all 0.18s;
      font-family: 'Lora', serif;
    }
    .mini-btn:hover { background: var(--text-color); color: var(--bg-color); }
    .loop-active { background: var(--accent); color: #fff; border-color: var(--accent); }

    /* ── VOCAB SECTION ── */
    #vocabSection { margin-top: 30px; border-top: 1px dashed var(--border-color); padding-top: 22px; }
    #vocabList {
      display: flex;
      flex-wrap: wrap;
      gap: 8px;
      margin-top: 12px;
      min-height: 120px;
      padding: 18px;
      border: 1px dashed var(--border-color);
      border-radius: 8px;
      background: var(--bg-color);
      align-content: flex-start;
      position: relative;
    }

    .vocab-item {
      background: var(--card-bg);
      border: 1px solid var(--accent);
      color: var(--text-color);
      padding: 5px 12px;
      border-radius: 40px;
      cursor: pointer;
      user-select: none;
      transition: all 0.18s;
      display: inline-flex;
      align-items: center;
      max-width: 220px;
      vertical-align: middle;
      position: relative;
      font-family: 'Lora', serif;
      font-size: 0.88rem;
    }
    .vocab-item[draggable="true"] { cursor: grab; }
    .vocab-item.dragging { opacity: 0.4; border-style: dashed; cursor: grabbing; }
    .vocab-item.drag-over-left  { border-left: 4px solid var(--accent); }
    .vocab-item.drag-over-right { border-right: 4px solid var(--accent); }
    .vocab-item:hover { transform: translateY(-2px); box-shadow: 0 3px 10px rgba(176,90,47,0.15); }

    .vocab-text {
      white-space: nowrap;
      overflow: hidden;
      text-overflow: ellipsis;
      display: block;
    }
    .vocab-idx {
      font-size: 0.72em;
      margin-right: 7px;
      font-family: 'Lora', serif;
      font-weight: 600;
      opacity: 0.45;
      color: var(--text-color);
      flex-shrink: 0;
    }

    /* Vocab column layouts */
    #vocabList.group-2 { display: block; column-count: 2; column-gap: 30px; column-rule: 1px dashed var(--border-color); }
    #vocabList.group-2 .vocab-item { display: inline-flex; width: calc(50% - 5px); max-width: none; margin-bottom: 8px; vertical-align: top; break-inside: avoid; box-sizing: border-box; }
    #vocabList.group-2 .vocab-item:nth-child(even)  { margin-left: 10px; }
    #vocabList.group-2 .vocab-item:nth-child(odd)   { margin-left: 0; break-after: avoid; }
    #vocabList.group-3 { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; align-content: flex-start; }
    #vocabList.group-4 { display: grid; grid-template-columns: repeat(4, 1fr); gap: 10px; align-content: flex-start; }
    #vocabList.group-3 .vocab-item, #vocabList.group-4 .vocab-item { max-width: 100%; width: auto; margin: 0; min-width: 0; }

    .group-2 .vocab-item:nth-child(odd)   .vocab-idx { opacity: 1; color: var(--accent); }
    .group-3 .vocab-item:nth-child(3n+1)  .vocab-idx { opacity: 1; color: var(--accent); }
    .group-4 .vocab-item:nth-child(4n+1)  .vocab-idx { opacity: 1; color: var(--accent); }

    .empty-msg {
      color: var(--muted);
      font-style: italic;
      font-family: 'Lora', serif;
      font-size: 0.95em;
      position: absolute;
      top: 50%; left: 50%;
      transform: translate(-50%, -50%);
      text-align: center;
      width: 100%;
      pointer-events: none;
    }

    .btn-sm {
      background: none;
      border: none;
      color: var(--accent);
      cursor: pointer;
      font-weight: 600;
      padding: 0;
      font-family: 'Lora', serif;
      font-size: 0.85em;
      transition: opacity 0.2s;
    }
    .btn-sm:hover { opacity: 0.7; }
    .btn-sm.danger { color: var(--danger); }
    .hidden { display: none !important; }

    /* ── ZEN MODE ── */
    body.zen-mode { overflow: hidden; }
    body.zen-mode #readerDisplay,
    body.zen-mode #textInput,
    body.zen-mode #verticalEditorWrapper,
    body.zen-mode #gridEditorWrapper {
      position: fixed; top: 0; left: 0; right: 0;
      width: 100% !important; height: calc(100vh - 80px) !important;
      margin: 0; border: none; border-radius: 0; z-index: 1500;
      padding: 48px; padding-bottom: 24px; box-sizing: border-box;
      background: var(--card-bg); overflow-y: auto !important;
      font-family: 'Lora', serif; font-size: 1.1rem; line-height: 1.4;
    }
    body.zen-mode ::-webkit-scrollbar { width: 8px; }
    body.zen-mode ::-webkit-scrollbar-track { background: transparent; }
    body.zen-mode ::-webkit-scrollbar-thumb { background-color: var(--border-color); border-radius: 4px; }
    body.zen-mode .btns {
      position: fixed; bottom: 0; left: 0; width: 100%;
      z-index: 1501; margin: 0; padding: 14px 20px; border-radius: 0;
    }

    /* ── TOAST ── */
    #toast {
      visibility: hidden; min-width: 220px; transform: translateX(-50%);
      background-color: var(--text-color); color: var(--bg-color);
      text-align: center; border-radius: 40px; padding: 10px 24px;
      position: fixed; z-index: 10000; left: 50%; bottom: 30px;
      font-family: 'Lora', serif; font-size: 0.88rem; font-weight: 600;
      opacity: 0; transition: opacity 0.3s, bottom 0.3s;
      box-shadow: 0 4px 16px rgba(26,18,8,0.2); pointer-events: none;
    }
    #toast.show { visibility: visible; opacity: 1; bottom: 50px; }

    /* ── RUBY (Furigana/Pinyin) ── */
    ruby { ruby-position: over; ruby-align: center; }
    rt {
      user-select: none; pointer-events: none;
      font-weight: 600; color: var(--muted);
      font-family: 'Lora', serif; font-size: 0.62em;
      position: relative; z-index: 1;
    }
    .active-sentence rt { color: var(--text-color) !important; }

    /* ── MODALS ── */
    .modal-overlay {
      display: none; position: fixed; top: 0; left: 0; width: 100%; height: 100%;
      background: rgba(26,18,8,0.55); z-index: 2000;
      justify-content: center; align-items: center;
      backdrop-filter: blur(2px);
    }
    .modal-box {
      background: var(--card-bg); color: var(--text-color);
      padding: 28px; border-radius: 10px; max-width: 420px; width: 90%;
      box-shadow: 0 12px 40px rgba(26,18,8,0.25); position: relative;
      border: 1px solid var(--border-color);
      font-family: 'Lora', serif;
    }
    .modal-box h3 {
      font-family: 'Playfair Display', Georgia, serif;
      font-size: 1.15rem; margin-top: 0; margin-bottom: 14px;
      color: var(--text-color);
    }
    .modal-close {
      position: absolute; top: 12px; right: 16px;
      font-size: 22px; cursor: pointer; opacity: 0.4;
      color: var(--text-color); transition: opacity 0.18s;
    }
    .modal-close:hover { opacity: 1; }
    .key-row {
      display: flex; justify-content: space-between; margin-bottom: 8px;
      font-size: 0.85em; border-bottom: 1px dotted var(--border-color); padding-bottom: 5px;
    }
    .key-code {
      font-family: 'Lora', serif; background: var(--paper-dark);
      padding: 2px 8px; border-radius: 4px; font-weight: 600;
      font-size: 0.88em; border: 1px solid var(--border-color);
    }

    /* ── COMBO BOXES ── */
    .combo-wrapper {
      position: relative; display: flex; align-items: center;
      width: 100%; border: 1px solid var(--border-color);
      border-radius: 6px; background: var(--card-bg);
      transition: border-color 0.2s;
    }
    .combo-wrapper:focus-within { border-color: var(--accent); }
    .combo-input {
      flex: 1; border: none; background: transparent;
      padding: 9px 12px; color: var(--text-color);
      font-family: 'Lora', serif; font-size: 0.88rem;
      outline: none; min-width: 0;
    }
    .combo-input::placeholder { color: var(--muted); font-style: italic; }
    .combo-arrow {
      padding: 0 10px; cursor: pointer; color: var(--accent);
      background: transparent; border: none; font-size: 0.75em; user-select: none;
    }
    .combo-dropdown {
      position: absolute; top: 100%; left: 0; right: 0;
      background: var(--card-bg); border: 1px solid var(--border-color);
      border-radius: 0 0 6px 6px; max-height: 200px; overflow-y: auto;
      z-index: 1000; display: none; box-shadow: 0 6px 20px rgba(26,18,8,0.1);
    }
    .combo-dropdown.show { display: block; }
    .combo-option {
      padding: 9px 12px; cursor: pointer;
      border-bottom: 1px solid var(--border-color);
      font-size: 0.85em; font-family: 'Lora', serif;
      white-space: normal; line-height: 1.3; color: var(--text-color);
      transition: background 0.15s;
    }
    .combo-option:hover { background: var(--accent); color: #fff; }
    .combo-option:last-child { border-bottom: none; }

    /* ── QUICK NAV SIDEBAR ── */
    .quick-nav {
      position: fixed; left: 14px; top: 50%; transform: translateY(-50%);
      display: flex; flex-direction: column; gap: 10px; z-index: 100;
      background: var(--card-bg); padding: 12px;
      border-radius: 12px; border: 1px solid var(--border-color);
      box-shadow: 0 4px 16px rgba(26,18,8,0.07);
    }
    .quick-nav-btn {
      background: transparent; color: var(--text-color);
      border: none; padding: 9px; border-radius: 8px;
      cursor: pointer; font-family: 'Lora', serif;
      font-size: 0.78em; display: flex; flex-direction: column;
      align-items: center; gap: 4px; transition: all 0.18s;
    }
    .quick-nav-btn:hover { background: var(--paper-dark); color: var(--accent); transform: translateY(-2px); }
    .quick-nav-btn span.icon { font-size: 1.3em; }
    @media (max-width: 1100px) { .quick-nav { display: none; } }

    /* ── VERTICAL EDITOR ── */
    #verticalEditor {
      display: flex; flex-direction: column; gap: 6px;
      max-height: 420px; overflow-y: auto; padding: 10px;
      background: var(--bg-color); border: 1px solid var(--border-color);
      border-radius: 8px; counter-reset: vertical-row-counter;
    }
    #verticalEditor::-webkit-scrollbar { width: 6px; }
    #verticalEditor::-webkit-scrollbar-thumb { background: var(--border-color); border-radius: 3px; }
    
    .vertical-row {
      display: flex; align-items: stretch; gap: 8px;
      background: var(--card-bg); padding: 6px; border-radius: 6px;
      border: 1px solid var(--border-color); transition: border-color 0.2s;
    }
    .vertical-row:hover { border-color: rgba(176,90,47,0.3); }
    .vertical-row::before {
      counter-increment: vertical-row-counter;
      content: counter(vertical-row-counter) ".";
      width: 24px; text-align: right; font-size: 0.8em;
      color: var(--muted); padding-top: 8px; display: flex;
    }
    .vertical-voice-select {
        border: 1px solid var(--border-color); background: var(--bg-color);
        color: var(--accent); border-radius: 4px; padding: 2px 4px;
        font-size: 0.85em; font-weight: bold; height: fit-content;
        margin-top: 4px; cursor: pointer; outline: none;
        width: auto; max-width: 140px; text-overflow: ellipsis;
    }
    .vertical-cell {
      flex: 1; min-height: 36px; padding: 6px 8px;
      border: 1px solid transparent; border-radius: 4px;
      background: transparent; outline: none; font-size: 0.95rem;
      color: var(--text-color); cursor: text;
    }
    .vertical-cell:focus { border-color: var(--accent); background: var(--bg-color); }
    .vertical-cell:empty:before { content: attr(data-placeholder); color: var(--muted); pointer-events: none; font-style: italic; }

    /* ── GRID EDITOR ── */
    #gridEditorWrapper { margin-bottom: 20px; }
    #gridEditor {
      display: flex; flex-direction: column; gap: 6px;
      max-height: 420px; overflow-y: auto; padding: 10px;
      background: var(--bg-color); border: 1px solid var(--border-color);
      border-radius: 8px; counter-reset: grid-row-counter;
    }
    #gridEditor::-webkit-scrollbar { width: 6px; }
    #gridEditor::-webkit-scrollbar-thumb { background: var(--border-color); border-radius: 3px; }

    .grid-row {
      display: grid; grid-template-columns: 24px 1fr 1fr 1fr auto;
      gap: 8px; align-items: start;
    }
    .grid-row::before {
      counter-increment: grid-row-counter;
      content: counter(grid-row-counter) ".";
      font-weight: 600; color: var(--accent); padding-top: 10px;
      text-align: right; font-size: 0.82rem; opacity: 0.65;
      user-select: none; font-family: 'Lora', serif;
    }
    .grid-cell {
      border: 1px solid var(--border-color); padding: 9px 11px;
      border-radius: 5px; min-height: 42px; outline: none;
      font-size: 0.88rem; font-family: 'Lora', serif; line-height: 1.6;
      background: var(--card-bg); color: var(--text-color);
      white-space: pre-wrap; word-break: break-word; flex: 1;
      transition: min-height 0.3s ease, border-color 0.2s;
    }
    .grid-cell:focus { border-color: var(--accent); }
    [contenteditable] { -webkit-user-modify: read-write-plaintext-only; }
    #gridEditor > .grid-row:only-child .grid-cell { min-height: 180px; }
    .grid-cell:empty:before { content: attr(data-placeholder); color: var(--muted); pointer-events: none; font-style: italic; }

    .cell-wrapper { position: relative; display: flex; flex-direction: column; height: 100%; }
    .cell-shift-actions { position: absolute; right: 2px; top: 2px; display: flex; flex-direction: column; opacity: 0; transition: opacity 0.2s; }
    .cell-wrapper:hover .cell-shift-actions { opacity: 1; }
    .cell-shift-btn {
      background: rgba(176,90,47,0.08); border: none; border-radius: 3px;
      font-size: 10px; cursor: pointer; padding: 2px 4px; margin-bottom: 2px;
      color: var(--text-color);
    }
    .cell-shift-btn:hover { background: var(--accent); color: white; }
    [data-theme="dark"] .cell-shift-btn { background: rgba(201,122,74,0.15); color: var(--text-color); }
    [data-theme="dark"] .cell-shift-btn:hover { background: var(--accent); }

    .grid-actions { display: flex; flex-direction: column; gap: 4px; height: 100%; justify-content: flex-start; padding-top: 5px; }
    .grid-action-btn {
      background: transparent; border: none; cursor: pointer;
      font-size: 16px; padding: 2px 4px;
      display: flex; align-items: center; justify-content: center;
      border-radius: 4px; transition: background 0.2s;
    }
    .grid-action-btn:hover { background: rgba(176,90,47,0.08); }
    [data-theme="dark"] .grid-action-btn:hover { background: rgba(201,122,74,0.12); }
    .grid-delete-btn { color: var(--danger); }
    .grid-insert-btn { color: var(--success); font-size: 13px; }
