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

:root {
  --bg:          #F7F5F0;
  --surface:     #FFFFFF;
  --border:      #E8E5DF;
  --border-soft: #F0EDE8;
  --text:        #171614;
  --text-muted:  #8A8580;
  --text-faint:  #BAB6B0;
  --accent:      #C8281E;
  --accent-soft: #F5ECEB;
  --green:       #2E7D4F;
  --green-soft:  #EBF5EF;
  --font-ui:   'Bricolage Grotesque', sans-serif;
  --font-mono: 'Space Mono', monospace;
  --font-body: 'Lora', Georgia, serif;
  --font-thai: 'Noto Sans Thai', 'Leelawadee UI', 'Sarabun', system-ui, sans-serif;
  --radius: 14px;
  --nav-height: 62px;
  --header-height: 54px;
  --cat-height: 46px;
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

html, body { height: 100%; background: var(--bg); color: var(--text); font-family: var(--font-ui); -webkit-font-smoothing: antialiased; overscroll-behavior: none; }

#app { display: flex; flex-direction: column; height: 100dvh; max-width: 480px; margin: 0 auto; background: var(--bg); overflow: hidden; }

/* ─── Header ────────────────────────────────────────────── */
#app-header { height: var(--header-height); background: var(--bg); border-bottom: 1px solid var(--border); flex-shrink: 0; z-index: 10; }
.header-inner { display: flex; align-items: center; justify-content: space-between; padding: 0 14px 0 10px; height: 100%; }
.header-left { display: flex; align-items: center; gap: 8px; }
.header-title { display: flex; align-items: baseline; gap: 8px; }
.header-script { font-family: var(--font-thai); font-size: 18px; font-weight: 600; letter-spacing: 0.02em; }
.header-tagline { font-size: 11px; color: var(--text-muted); letter-spacing: 0.08em; text-transform: uppercase; }

.overview-btn { width: 36px; height: 36px; border-radius: 8px; border: 1px solid var(--border); background: transparent; color: var(--text-muted); cursor: pointer; display: flex; align-items: center; justify-content: center; transition: all 0.2s; -webkit-tap-highlight-color: transparent; flex-shrink: 0; }
.overview-btn:active { background: var(--text); color: var(--bg); }

.speak-all-btn { width: 36px; height: 36px; border-radius: 50%; border: 1px solid var(--border); background: transparent; color: var(--text-muted); cursor: pointer; display: flex; align-items: center; justify-content: center; transition: all 0.2s; -webkit-tap-highlight-color: transparent; }
.speak-all-btn:active, .speak-all-btn.speaking { background: var(--accent-soft); border-color: var(--accent); color: var(--accent); }

/* ─── Category Nav ──────────────────────────────────────── */
#category-nav { height: var(--cat-height); background: var(--bg); border-bottom: 1px solid var(--border); flex-shrink: 0; }
.cat-scroll { height: 100%; overflow-x: auto; overflow-y: hidden; -webkit-overflow-scrolling: touch; scrollbar-width: none; }
.cat-scroll::-webkit-scrollbar { display: none; }
.cat-tabs { display: flex; align-items: center; gap: 4px; padding: 0 14px; height: 100%; white-space: nowrap; }
.cat-tab { display: inline-flex; align-items: center; padding: 5px 14px; border: 1px solid var(--border); border-radius: 20px; background: transparent; color: var(--text-muted); font-family: var(--font-ui); font-size: 12px; font-weight: 400; cursor: pointer; transition: all 0.2s; -webkit-tap-highlight-color: transparent; }
.cat-tab:active { transform: scale(0.96); }
.cat-tab.active { background: var(--text); border-color: var(--text); color: var(--bg); font-weight: 600; }

/* ─── Card Area ─────────────────────────────────────────── */
#card-area { flex: 1; display: flex; flex-direction: column; overflow: hidden; padding: 12px 16px 10px; gap: 10px; }
.card-meta { display: flex; align-items: center; justify-content: flex-end; padding: 0 2px; flex-shrink: 0; min-height: 16px; }
.card-counter { font-family: var(--font-mono); font-size: 10px; color: var(--text-faint); letter-spacing: 0.05em; }
.card-viewport { flex: 1; overflow: hidden; position: relative; border-radius: var(--radius); }
.card-container { height: 100%; position: relative; }

/* ─── Card ──────────────────────────────────────────────── */
.card { position: absolute; inset: 0; background: var(--surface); border-radius: var(--radius); border: 1px solid var(--border); display: flex; flex-direction: column; overflow: hidden; }
@keyframes slideInRight { from { transform: translateX(55px); opacity: 0; } to { transform: translateX(0); opacity: 1; } }
@keyframes slideInLeft  { from { transform: translateX(-55px); opacity: 0; } to { transform: translateX(0); opacity: 1; } }
@keyframes fadeIn       { from { opacity: 0; transform: translateY(6px); } to { opacity: 1; transform: translateY(0); } }
.card.slide-in-right { animation: slideInRight 0.22s cubic-bezier(0.25,0.46,0.45,0.94) both; }
.card.slide-in-left  { animation: slideInLeft  0.22s cubic-bezier(0.25,0.46,0.45,0.94) both; }
.card.fade-in        { animation: fadeIn 0.25s ease both; }

.card-top { display: flex; align-items: center; justify-content: space-between; padding: 14px 16px 0; flex-shrink: 0; }
.card-tag { font-size: 10px; letter-spacing: 0.1em; text-transform: uppercase; color: var(--text-faint); font-weight: 600; }

.card-main { flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center; padding: 10px 20px 14px; text-align: center; gap: 8px; min-height: 0; }
.card-thai { font-family: var(--font-thai); font-size: clamp(42px, 14vw, 72px); line-height: 1.15; color: var(--text); letter-spacing: 0.04em; font-weight: 400; word-break: break-word; }
.card-thai.multi { font-size: clamp(26px, 8vw, 48px); line-height: 1.35; }
.card-phonetic { font-family: var(--font-mono); font-size: 13px; color: var(--accent); letter-spacing: 0.04em; }
.card-phonetic.api-note { color: var(--text-faint) !important; font-size: 10px !important; font-style: italic; }
.card-label { font-size: 12px; color: var(--text-faint); letter-spacing: 0.04em; }
.card-meaning { font-family: var(--font-ui); font-size: 15px; color: var(--text-muted); line-height: 1.4; max-width: 280px; }

/* ─── Word Chips ────────────────────────────────────────── */
.word-chips { display: flex; flex-wrap: wrap; gap: 8px; justify-content: center; align-items: center; padding: 0 4px; }
.word-chip { font-family: var(--font-thai); font-size: clamp(20px, 6.5vw, 32px); line-height: 1.3; color: var(--text); background: var(--bg); border: 1.5px solid var(--border); border-radius: 10px; padding: 4px 10px; cursor: pointer; transition: all 0.15s ease; -webkit-tap-highlight-color: transparent; user-select: none; }
.word-chip:active, .word-chip.tapped { background: var(--text); color: var(--bg); border-color: var(--text); transform: scale(0.95); }
.word-chip.highlighted { background: var(--accent-soft); border-color: var(--accent); color: var(--accent); }
.word-chips.explore .word-chip { font-size: clamp(17px, 5.5vw, 28px); }

.sentence-phonetic { font-family: var(--font-mono); font-size: 12px; color: var(--accent); letter-spacing: 0.03em; text-align: center; padding: 0 12px; line-height: 1.5; }
.sentence-meaning { font-family: var(--font-ui); font-size: 14px; color: var(--text-muted); text-align: center; max-width: 280px; line-height: 1.4; }

/* ─── Expand ────────────────────────────────────────────── */
.expand-btn { width: 100%; padding: 11px 16px; border: none; border-top: 1px solid var(--border-soft); background: transparent; color: var(--text-muted); font-family: var(--font-ui); font-size: 12px; letter-spacing: 0.06em; text-transform: uppercase; cursor: pointer; display: flex; align-items: center; justify-content: center; gap: 6px; flex-shrink: 0; transition: background 0.15s, color 0.15s; -webkit-tap-highlight-color: transparent; }
.expand-btn:active { background: var(--bg); }
.expand-btn svg { transition: transform 0.25s; }
.expand-btn.open svg { transform: rotate(180deg); }
.expand-btn.open { color: var(--accent); }
.card-detail { background: var(--bg); border-top: 1px solid var(--border-soft); max-height: 0; overflow-y: auto; -webkit-overflow-scrolling: touch; transition: max-height 0.35s cubic-bezier(0.4,0,0.2,1); flex-shrink: 0; }
.card-detail.open { max-height: 220px; }
.detail-text { padding: 14px 18px 18px; }
.detail-text p { font-family: var(--font-body); font-size: 13.5px; line-height: 1.7; color: var(--text-muted); margin-bottom: 10px; }
.detail-text p:last-child { margin-bottom: 0; }
.card-detail::-webkit-scrollbar { width: 3px; }
.card-detail::-webkit-scrollbar-thumb { background: var(--border); border-radius: 2px; }

/* ─── Card Nav ──────────────────────────────────────────── */
.card-nav { display: flex; align-items: center; justify-content: center; gap: 10px; flex-shrink: 0; }

.next-btn { display: flex; align-items: center; gap: 7px; padding: 0 22px; height: 44px; border-radius: 22px; border: 1.5px solid var(--text); background: var(--text); color: var(--bg); font-family: var(--font-ui); font-size: 13px; font-weight: 600; letter-spacing: 0.05em; cursor: pointer; transition: all 0.2s; -webkit-tap-highlight-color: transparent; }
.next-btn:active { background: var(--accent); border-color: var(--accent); transform: scale(0.96); }

.random-section-btn { display: flex; align-items: center; gap: 7px; padding: 0 18px; height: 44px; border-radius: 22px; border: 1.5px solid var(--border); background: transparent; color: var(--text-muted); font-family: var(--font-ui); font-size: 13px; font-weight: 600; letter-spacing: 0.05em; cursor: pointer; transition: all 0.2s; -webkit-tap-highlight-color: transparent; }
.random-section-btn:active { background: var(--bg); border-color: var(--text-muted); transform: scale(0.96); }

/* ─── Bottom Nav ────────────────────────────────────────── */
#bottom-nav { display: flex; height: var(--nav-height); padding-bottom: var(--safe-bottom); border-top: 1px solid var(--border); background: var(--surface); flex-shrink: 0; }
.tab-btn { flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 3px; background: none; border: none; cursor: pointer; padding: 8px 4px; color: var(--text-faint); transition: color 0.2s; -webkit-tap-highlight-color: transparent; }
.tab-btn.active { color: var(--text); }
.tab-btn:active { opacity: 0.7; }
.tab-icon { font-family: var(--font-thai); font-size: 19px; line-height: 1; transition: transform 0.2s; }
.tab-btn.active .tab-icon { transform: scale(1.08); }
.tab-label { font-size: 9.5px; letter-spacing: 0.06em; text-transform: uppercase; font-weight: 600; }

/* ─── Word Panel ────────────────────────────────────────── */
.word-overlay { position: fixed; inset: 0; background: rgba(23,22,20,0); pointer-events: none; z-index: 50; transition: background 0.25s; }
.word-overlay.visible { background: rgba(23,22,20,0.35); pointer-events: all; }
.word-panel { position: fixed; bottom: 0; left: 50%; transform: translateX(-50%) translateY(100%); width: 100%; max-width: 480px; background: var(--surface); border-radius: 20px 20px 0 0; border-top: 1px solid var(--border); z-index: 51; transition: transform 0.3s cubic-bezier(0.32,0.72,0,1); padding-bottom: var(--safe-bottom); }
.word-panel.open { transform: translateX(-50%) translateY(0); }
.wp-handle { width: 36px; height: 4px; border-radius: 2px; background: var(--border); margin: 12px auto 0; }
.wp-body { padding: 16px 24px 28px; display: flex; flex-direction: column; align-items: center; gap: 7px; text-align: center; }
.wp-thai { font-family: var(--font-thai); font-size: 46px; line-height: 1.2; color: var(--text); }
.wp-phonetic { font-family: var(--font-mono); font-size: 14px; color: var(--accent); letter-spacing: 0.04em; min-height: 20px; }
.wp-meaning { font-family: var(--font-ui); font-size: 16px; color: var(--text-muted); line-height: 1.4; max-width: 280px; }
.wp-context { font-family: var(--font-ui); font-size: 11px; color: var(--text-faint); font-style: italic; max-width: 260px; line-height: 1.4; }
.wp-speak-btn { display: flex; align-items: center; gap: 7px; padding: 9px 22px; border-radius: 20px; border: 1.5px solid var(--border); background: transparent; color: var(--text-muted); font-family: var(--font-ui); font-size: 12px; font-weight: 600; letter-spacing: 0.05em; cursor: pointer; transition: all 0.2s; margin-top: 4px; }
.wp-speak-btn:active, .wp-speak-btn.speaking { background: var(--accent-soft); border-color: var(--accent); color: var(--accent); }

/* ─── Overview Panel ────────────────────────────────────── */
.ov-overlay { position: fixed; inset: 0; background: rgba(23,22,20,0); pointer-events: none; z-index: 60; transition: background 0.3s; }
.ov-overlay.visible { background: rgba(23,22,20,0.5); pointer-events: all; }

.ov-panel { position: fixed; inset: 0; left: 0; width: 100%; max-width: 480px; left: 50%; transform: translateX(-50%) translateX(-100%); background: var(--surface); z-index: 61; display: flex; flex-direction: column; transition: transform 0.32s cubic-bezier(0.32,0.72,0,1); }
.ov-panel.open { transform: translateX(-50%) translateX(0); }

.ov-header { flex-shrink: 0; padding: 16px 16px 12px; border-bottom: 1px solid var(--border); display: flex; flex-direction: column; gap: 10px; }
.ov-header-row { display: flex; align-items: center; justify-content: space-between; }
.ov-title { font-family: var(--font-ui); font-size: 17px; font-weight: 600; color: var(--text); }
.ov-close-btn { width: 34px; height: 34px; border-radius: 50%; border: 1px solid var(--border); background: transparent; color: var(--text-muted); cursor: pointer; display: flex; align-items: center; justify-content: center; transition: all 0.2s; -webkit-tap-highlight-color: transparent; }
.ov-close-btn:active { background: var(--text); color: var(--bg); }

.ov-search { width: 100%; padding: 9px 14px; border: 1px solid var(--border); border-radius: 22px; background: var(--bg); font-family: var(--font-ui); font-size: 13px; color: var(--text); outline: none; transition: border-color 0.2s; }
.ov-search:focus { border-color: var(--text); }
.ov-search::placeholder { color: var(--text-faint); }

.ov-list { flex: 1; overflow-y: auto; -webkit-overflow-scrolling: touch; padding: 0 0 var(--safe-bottom); }
.ov-list::-webkit-scrollbar { width: 3px; }
.ov-list::-webkit-scrollbar-thumb { background: var(--border); border-radius: 2px; }

.ov-section-label { padding: 12px 16px 6px; font-size: 10px; letter-spacing: 0.1em; text-transform: uppercase; color: var(--text-faint); font-weight: 700; background: var(--bg); font-family: var(--font-ui); position: sticky; top: 0; z-index: 1; border-bottom: 1px solid var(--border-soft); }

.ov-row { display: flex; align-items: center; gap: 12px; padding: 11px 16px; border-bottom: 1px solid var(--border-soft); -webkit-tap-highlight-color: transparent; }
.ov-row:active { background: var(--bg); }

.ov-thai { font-family: var(--font-thai); font-size: 22px; line-height: 1.2; color: var(--text); min-width: 60px; flex-shrink: 0; }
.ov-info { flex: 1; min-width: 0; display: flex; flex-direction: column; gap: 2px; }
.ov-phonetic { font-family: var(--font-mono); font-size: 10px; color: var(--accent); letter-spacing: 0.03em; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.ov-meaning { font-family: var(--font-ui); font-size: 12px; color: var(--text-muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.ov-tag { font-family: var(--font-ui); font-size: 9px; letter-spacing: 0.06em; text-transform: uppercase; color: var(--text-faint); padding: 2px 6px; border: 1px solid var(--border); border-radius: 8px; white-space: nowrap; flex-shrink: 0; }
.ov-speak { width: 30px; height: 30px; border-radius: 50%; border: 1px solid var(--border); background: transparent; color: var(--text-faint); cursor: pointer; display: flex; align-items: center; justify-content: center; flex-shrink: 0; transition: all 0.15s; -webkit-tap-highlight-color: transparent; }
.ov-speak:active { background: var(--accent-soft); border-color: var(--accent); color: var(--accent); }

.ov-empty { padding: 40px 24px; text-align: center; color: var(--text-faint); font-family: var(--font-ui); font-size: 14px; }

/* ─── Loading / Retry ───────────────────────────────────── */
.loading-spinner { width: 34px; height: 34px; border: 2.5px solid var(--border); border-top-color: var(--text); border-radius: 50%; animation: spin 0.8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
.retry-btn { margin-top: 16px; padding: 9px 22px; border-radius: 20px; border: 1.5px solid var(--text); background: transparent; color: var(--text); font-family: var(--font-ui); font-size: 12px; font-weight: 600; letter-spacing: 0.05em; cursor: pointer; transition: all 0.2s; }
.retry-btn:active { background: var(--text); color: var(--bg); }

/* ─── Toast ─────────────────────────────────────────────── */
#toast-container { position: fixed; bottom: calc(var(--nav-height) + 14px + var(--safe-bottom)); left: 50%; transform: translateX(-50%); z-index: 100; pointer-events: none; display: flex; flex-direction: column; gap: 8px; align-items: center; }
.toast { background: var(--text); color: var(--bg); padding: 9px 18px; border-radius: 20px; font-family: var(--font-ui); font-size: 13px; animation: toastIn 0.25s ease forwards; white-space: nowrap; }
@keyframes toastIn { from { opacity: 0; transform: translateY(8px) scale(0.95); } to { opacity: 1; transform: translateY(0) scale(1); } }

@media (min-width: 480px) {
  #app { box-shadow: 0 0 0 1px var(--border); }
  body { background: #EDEAE4; }
  .ov-panel { left: 50%; border-right: 1px solid var(--border); }
}
