/* ============================================================
   Orthomots — design system
   Vanilla CSS, light & dark themes, responsive, print-ready.
   ============================================================ */

:root {
  --indigo: #4f46e5;
  --indigo-600: #4338ca;
  --radius: 16px;
  --radius-sm: 10px;
  --radius-pill: 999px;
  --maxw: 1560px;
  --topbar-h: 64px;
  font-family: 'Inter', system-ui, -apple-system, 'Segoe UI', Roboto, sans-serif;
  -webkit-text-size-adjust: 100%;
}

/* ---- Light theme (default) ---- */
:root, [data-theme="light"] {
  --bg: #f4f5fb;
  --bg-elev: #eceef7;
  --surface: #ffffff;
  --surface-2: #f8f9fd;
  --text: #1b1f2a;
  --muted: #6b7280;
  --faint: #9aa1ad;
  --line: #e4e6ef;
  --line-strong: #d4d7e3;
  --primary: var(--indigo);
  --primary-hover: var(--indigo-600);
  --primary-text: #ffffff;
  --primary-soft: #eef0ff;
  --primary-soft-line: #c8ccfb;
  --warn: #e5484d;
  --shadow-sm: 0 1px 2px rgba(20,22,40,.06);
  --shadow-md: 0 8px 28px rgba(20,22,40,.10);
  --shadow-lg: 0 18px 50px rgba(20,22,40,.18);
}

/* ---- Dark theme ---- */
[data-theme="dark"] {
  --bg: #0f1117;
  --bg-elev: #161a23;
  --surface: #1a1e29;
  --surface-2: #20252f;
  --text: #e8eaf0;
  --muted: #9aa1b1;
  --faint: #6c7384;
  --line: #2a2f3c;
  --line-strong: #353b4a;
  --primary: #7c7bff;
  --primary-hover: #908fff;
  --primary-text: #0f1117;
  --primary-soft: #232547;
  --primary-soft-line: #3a3d6b;
  --warn: #ff6166;
  --shadow-sm: 0 1px 2px rgba(0,0,0,.4);
  --shadow-md: 0 8px 28px rgba(0,0,0,.5);
  --shadow-lg: 0 18px 50px rgba(0,0,0,.6);
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; }
body {
  background: var(--bg); color: var(--text);
  min-height: 100vh;
  transition: background .2s ease, color .2s ease;
}
button { font: inherit; cursor: pointer; color: inherit; }
:focus-visible { outline: 2px solid var(--primary); outline-offset: 2px; border-radius: 4px; }

/* ───────────────────────── Buttons ───────────────────────── */
.btn-primary, .btn-secondary, .btn-link {
  border: 1px solid transparent; border-radius: var(--radius-sm);
  padding: 9px 15px; font-weight: 600; font-size: 14px;
  display: inline-flex; align-items: center; gap: 7px; white-space: nowrap;
  transition: background .15s, border-color .15s, color .15s, transform .08s;
}
.btn-primary { background: var(--primary); color: var(--primary-text); box-shadow: var(--shadow-sm); }
.btn-primary:hover { background: var(--primary-hover); }
.btn-primary:active { transform: translateY(1px); }
.btn-secondary { background: var(--surface); color: var(--text); border-color: var(--line-strong); }
.btn-secondary:hover { background: var(--primary-soft); border-color: var(--primary); color: var(--primary); }
.btn-link { background: transparent; color: var(--primary); padding: 7px 4px; }
.btn-link:hover { text-decoration: underline; }
.btn-link.warn { color: var(--warn); }

.icon-btn {
  background: transparent; border: 1px solid transparent; border-radius: var(--radius-sm);
  width: 40px; height: 40px; font-size: 18px; line-height: 1;
  display: inline-flex; align-items: center; justify-content: center;
  transition: background .15s, border-color .15s;
}
.icon-btn:hover { background: var(--bg-elev); }

.count-badge {
  background: var(--primary); color: var(--primary-text);
  border-radius: var(--radius-pill); font-size: 12px; font-weight: 700;
  min-width: 20px; height: 20px; padding: 0 6px;
  display: inline-flex; align-items: center; justify-content: center;
}

/* ───────────────────────── Top bar ───────────────────────── */
.topbar {
  position: sticky; top: 0; z-index: 30;
  display: flex; align-items: center; gap: 18px;
  padding: 10px max(16px, env(safe-area-inset-left)) 10px max(16px, env(safe-area-inset-right));
  min-height: var(--topbar-h);
  background: color-mix(in srgb, var(--surface) 88%, transparent);
  backdrop-filter: saturate(140%) blur(10px);
  border-bottom: 1px solid var(--line);
}
.brand { display: flex; align-items: center; gap: 11px; flex-shrink: 0; }
.brand .logo { font-size: 26px; }
.brand-text { display: flex; flex-direction: column; line-height: 1.15; }
.brand .title { font-weight: 800; font-size: 19px; letter-spacing: -0.02em; }
.brand .tagline { color: var(--muted); font-size: 12px; }

.searchbar {
  flex: 1; max-width: 640px; margin: 0 auto;
  position: relative; display: flex; align-items: center;
}
.searchbar .search-icon {
  position: absolute; left: 14px; width: 18px; height: 18px; color: var(--faint);
  pointer-events: none;
}
.searchbar input {
  width: 100%; padding: 11px 40px 11px 42px;
  border: 1px solid var(--line-strong); border-radius: var(--radius-pill);
  background: var(--surface-2); color: var(--text); font: inherit; font-size: 15px;
  transition: border-color .15s, background .15s, box-shadow .15s;
}
.searchbar input::placeholder { color: var(--faint); }
.searchbar input:focus {
  outline: none; border-color: var(--primary); background: var(--surface);
  box-shadow: 0 0 0 4px var(--primary-soft);
}
.q-clear {
  position: absolute; right: 8px; width: 26px; height: 26px; border-radius: 50%;
  border: none; background: var(--bg-elev); color: var(--muted); font-size: 18px;
  display: flex; align-items: center; justify-content: center;
}
.q-clear:hover { background: var(--line-strong); color: var(--text); }

.top-actions { display: flex; align-items: center; gap: 10px; flex-shrink: 0; }
.basket-btn { padding: 9px 14px; }
/* theme icon swap */
[data-theme="light"] .theme-icon-dark, [data-theme="dark"] .theme-icon-light { display: none; }

/* ───────────────── Mobile filters trigger ───────────────── */
.filters-toggle {
  display: none; align-items: center; gap: 8px;
  position: sticky; top: var(--topbar-h); z-index: 20;
  width: 100%; padding: 12px 16px; border: none; border-bottom: 1px solid var(--line);
  background: var(--surface); font-weight: 600; font-size: 15px;
}
.mini-badge {
  background: var(--primary); color: var(--primary-text); border-radius: var(--radius-pill);
  font-size: 11px; font-weight: 700; padding: 1px 7px;
}

/* ───────────────────────── Layout ───────────────────────── */
.layout {
  display: grid; grid-template-columns: 288px 1fr;
  gap: 26px; padding: 24px; max-width: var(--maxw); margin: 0 auto;
}

/* ───────────────────────── Filters ───────────────────────── */
.filters {
  background: var(--surface); border: 1px solid var(--line);
  border-radius: var(--radius); padding: 20px;
  position: sticky; top: calc(var(--topbar-h) + 24px);
  align-self: start; max-height: calc(100vh - var(--topbar-h) - 48px); overflow-y: auto;
}
.filters-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 16px; }
.filters-head h2 { margin: 0; font-size: 13px; text-transform: uppercase; letter-spacing: .08em; color: var(--muted); }
.filters-close { display: none; }

.field { border: none; padding: 0; margin: 0 0 22px; display: block; }
.field legend, .field > span:first-child {
  font-weight: 600; font-size: 14px; margin-bottom: 10px; padding: 0;
  display: flex; align-items: center; gap: 8px;
}
.field .hint {
  font-size: 10px; font-weight: 700; letter-spacing: .04em;
  color: var(--muted); background: var(--bg-elev); border-radius: 5px; padding: 2px 6px;
}
.field select {
  width: 100%; padding: 10px 12px; border: 1px solid var(--line-strong);
  border-radius: var(--radius-sm); background: var(--surface-2); font: inherit; color: var(--text);
}
.field select:focus { outline: none; border-color: var(--primary); }

/* segmented control */
.seg { display: flex; background: var(--bg-elev); border-radius: var(--radius-sm); padding: 3px; margin-bottom: 12px; gap: 2px; }
.seg-btn {
  flex: 1; border: none; background: transparent; border-radius: 7px;
  padding: 7px 4px; font-size: 12.5px; font-weight: 600; color: var(--muted);
  transition: background .15s, color .15s;
}
.seg-btn.active { background: var(--surface); color: var(--primary); box-shadow: var(--shadow-sm); }

.phoneme-group { margin-bottom: 12px; }
.phoneme-group-label { display: block; font-size: 12px; color: var(--muted); margin-bottom: 6px; }
.chip-row { display: flex; flex-wrap: wrap; gap: 6px; }
.chip {
  border: 1px solid var(--line-strong); background: var(--surface);
  padding: 5px 11px; border-radius: var(--radius-pill); font-size: 13px;
  user-select: none; transition: all .12s; line-height: 1.3;
}
.chip:hover { border-color: var(--primary); color: var(--primary); }
.chip.active { background: var(--primary); border-color: var(--primary); color: var(--primary-text); }
.chip.excluded { background: #dc2626; border-color: #dc2626; color: #fff; text-decoration: line-through; }
.chip-hint { font-size: 11px; color: var(--muted, #64748b); margin: 2px 0 6px; }

/* toggle buttons (syllables, gender) */
.toggle-row { display: flex; flex-wrap: wrap; gap: 6px; }
.toggle-btn {
  flex: 1; min-width: 44px; border: 1px solid var(--line-strong); background: var(--surface);
  padding: 9px 10px; border-radius: var(--radius-sm); font-size: 14px; font-weight: 600;
  color: var(--text); transition: all .12s;
}
.toggle-btn:hover { border-color: var(--primary); color: var(--primary); }
.toggle-btn.active { background: var(--primary); border-color: var(--primary); color: var(--primary-text); }

/* ───────────────────── Active filters bar ───────────────────── */
.active-filters { display: flex; flex-wrap: wrap; gap: 8px; margin-bottom: 16px; }
.filter-tag {
  display: inline-flex; align-items: center; gap: 6px;
  background: var(--primary-soft); border: 1px solid var(--primary-soft-line);
  color: var(--primary); border-radius: var(--radius-pill);
  padding: 5px 6px 5px 12px; font-size: 13px; font-weight: 600;
}
.filter-tag button {
  border: none; background: transparent; color: inherit; font-size: 16px; line-height: 1;
  width: 18px; height: 18px; border-radius: 50%; display: flex; align-items: center; justify-content: center;
}
.filter-tag button:hover { background: var(--primary-soft-line); }
.filter-tag.clear-all { background: transparent; border-color: transparent; color: var(--muted); text-decoration: underline; cursor: pointer; }

/* ───────────────────── Results header ───────────────────── */
.results-header {
  display: flex; align-items: center; justify-content: space-between; gap: 16px;
  margin-bottom: 16px; flex-wrap: wrap;
}
.result-count { margin: 0; font-size: 18px; font-weight: 700; }
.results-tools { display: flex; align-items: center; gap: 12px; }
.size-control { display: flex; align-items: center; gap: 6px; color: var(--muted); }
.size-control input[type="range"] { width: 90px; accent-color: var(--primary); }

/* ───────────────────────── Grid ───────────────────────── */
.grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(var(--card-min, 170px), 1fr));
  gap: 14px;
}
.card {
  position: relative; background: var(--surface);
  border: 2px solid var(--line); border-radius: var(--radius);
  padding: 8px; display: flex; flex-direction: column; gap: 7px;
  cursor: pointer; user-select: none;
  transition: border-color .15s, box-shadow .15s, transform .1s;
}
.card:hover { border-color: var(--primary); box-shadow: var(--shadow-md); }
.card:active { transform: scale(.985); }
.card.selected { border-color: var(--primary); background: var(--primary-soft); }

.img-wrap {
  position: relative; width: 100%; aspect-ratio: 1;
  background: var(--surface-2); border-radius: var(--radius-sm); overflow: hidden;
  display: flex; align-items: center; justify-content: center;
}
.img-wrap img { max-width: 100%; max-height: 100%; object-fit: contain; }
.img-wrap.no-img { background: linear-gradient(135deg, var(--primary-soft), var(--surface-2)); }
.no-image-placeholder { font-size: 34px; opacity: .4; }

.label {
  font-weight: 700; font-size: 15px; text-align: center; word-break: break-word;
  padding: 0 2px;
}
.meta-row {
  display: flex; align-items: center; justify-content: center; gap: 6px;
  flex-wrap: wrap; min-height: 18px;
}
.meta {
  font-size: 10.5px; font-weight: 600; color: var(--muted);
  background: var(--bg-elev); border-radius: 5px; padding: 1px 6px; line-height: 1.5;
}
.meta.nat-NOM { color: #2563eb; } .meta.nat-VER { color: #16a34a; }
.meta.nat-ADJ { color: #d97706; } .meta.nat-ADV { color: #db2777; }
[data-theme="dark"] .meta.nat-NOM { color: #7aa2ff; } [data-theme="dark"] .meta.nat-VER { color: #5fd38a; }
[data-theme="dark"] .meta.nat-ADJ { color: #f0b75a; } [data-theme="dark"] .meta.nat-ADV { color: #ff85bd; }
.meta.ipa { font-family: 'Inter', sans-serif; letter-spacing: .02em; }

/* card corner controls */
.check {
  position: absolute; top: 10px; right: 10px; width: 24px; height: 24px;
  border-radius: 50%; background: var(--surface); border: 2px solid var(--line-strong);
  display: flex; align-items: center; justify-content: center; font-size: 13px; color: transparent;
  opacity: 0; transition: opacity .15s, background .15s, color .15s; z-index: 2;
}
.card:hover .check, .card.selected .check { opacity: 1; }
.card.selected .check { background: var(--primary); border-color: var(--primary); color: var(--primary-text); }

.zoom-btn, .variant-btn {
  position: absolute; top: 10px; width: 26px; height: 26px; border-radius: 50%;
  background: color-mix(in srgb, var(--surface) 80%, transparent); border: 1px solid var(--line);
  display: flex; align-items: center; justify-content: center; font-size: 13px;
  opacity: 0; transition: opacity .15s, background .15s; z-index: 2; backdrop-filter: blur(4px);
}
.zoom-btn { left: 10px; }
.variant-btn { left: 42px; font-size: 11px; gap: 1px; width: auto; padding: 0 7px; border-radius: var(--radius-pill); }
.card:hover .zoom-btn, .card:hover .variant-btn { opacity: 1; }
.zoom-btn:hover, .variant-btn:hover { background: var(--surface); border-color: var(--primary); color: var(--primary); }

.sentinel { height: 1px; }
.grid-status { text-align: center; color: var(--muted); padding: 24px; font-size: 14px; }

/* skeleton loading */
.skeleton {
  border: 2px solid var(--line); border-radius: var(--radius); padding: 8px;
  display: flex; flex-direction: column; gap: 7px;
}
.skeleton .sk-img { width: 100%; aspect-ratio: 1; border-radius: var(--radius-sm); }
.skeleton .sk-line { height: 14px; border-radius: 5px; width: 70%; margin: 0 auto; }
.sk-img, .sk-line {
  background: linear-gradient(90deg, var(--bg-elev) 25%, var(--surface-2) 37%, var(--bg-elev) 63%);
  background-size: 400% 100%; animation: shimmer 1.4s ease infinite;
}
@keyframes shimmer { 0% { background-position: 100% 0; } 100% { background-position: -100% 0; } }

.empty { grid-column: 1 / -1; text-align: center; color: var(--muted); padding: 64px 20px; }
.empty .big { font-size: 40px; display: block; margin-bottom: 12px; }

/* ───────────────── Floating selection bar ───────────────── */
.selbar {
  position: fixed; left: 50%; transform: translateX(-50%);
  bottom: max(18px, env(safe-area-inset-bottom)); z-index: 40;
  display: flex; align-items: center; gap: 16px;
  background: var(--surface); border: 1px solid var(--line-strong);
  border-radius: var(--radius-pill); box-shadow: var(--shadow-lg);
  padding: 10px 12px 10px 20px; max-width: calc(100vw - 32px);
  animation: pop .2s ease;
}
@keyframes pop { from { transform: translate(-50%, 16px); opacity: 0; } to { transform: translate(-50%, 0); opacity: 1; } }
.selbar-count { font-size: 14px; white-space: nowrap; }
.selbar-actions { display: flex; gap: 8px; flex-wrap: wrap; }

/* ───────────────────────── Dialogs ───────────────────────── */
.dialog {
  border: none; border-radius: var(--radius); padding: 0;
  width: min(1100px, 95vw); max-height: 90vh; background: var(--surface); color: var(--text);
  box-shadow: var(--shadow-lg);
}
.dialog-sm { width: min(440px, 95vw); }
.dialog::backdrop { background: rgba(10,12,22,.5); backdrop-filter: blur(3px); }
.dialog header {
  display: flex; align-items: center; justify-content: space-between;
  padding: 16px 20px; border-bottom: 1px solid var(--line); position: sticky; top: 0; background: var(--surface);
}
.dialog header h2 { margin: 0; font-size: 18px; display: flex; align-items: center; gap: 10px; }
.basket-actions {
  display: flex; gap: 8px; padding: 14px 20px; border-bottom: 1px solid var(--line); flex-wrap: wrap;
}
#basket-grid { padding: 20px; overflow-y: auto; max-height: 64vh; }

.print-form { padding: 20px; display: flex; flex-direction: column; gap: 16px; }
.print-form .field { margin: 0; }
.print-form .field span { display: block; margin-bottom: 6px; font-weight: 600; }
.checkrow { display: flex; align-items: center; gap: 10px; font-size: 14px; }
.checkrow input { width: 18px; height: 18px; accent-color: var(--primary); }
.print-foot { padding: 0 20px 20px; }

/* ───────────────────── Detail lightbox ───────────────────── */
.lightbox {
  position: fixed; inset: 0; z-index: 60; display: flex; align-items: center; justify-content: center;
  background: rgba(10,12,22,.72); backdrop-filter: blur(5px); padding: 24px;
}
.lb-card {
  background: var(--surface); border-radius: var(--radius); box-shadow: var(--shadow-lg);
  width: min(860px, 96vw); max-height: 92vh; overflow: auto; display: grid;
  grid-template-columns: 1.2fr 1fr;
}
.lb-imgcol { background: var(--surface-2); display: flex; align-items: center; justify-content: center; padding: 28px; }
.lb-imgcol img { max-width: 100%; max-height: 60vh; object-fit: contain; }
.lb-info { padding: 28px; display: flex; flex-direction: column; gap: 14px; }
.lb-name { font-size: 30px; font-weight: 800; letter-spacing: -.02em; }
.lb-ipa { color: var(--muted); font-size: 18px; }
.lb-attrs { display: flex; flex-wrap: wrap; gap: 8px; }
.lb-attr { background: var(--bg-elev); border-radius: var(--radius-sm); padding: 6px 12px; font-size: 13px; font-weight: 600; }
.lb-attr small { display: block; color: var(--muted); font-weight: 500; font-size: 11px; }
.lb-variants { display: flex; flex-wrap: wrap; gap: 8px; }
.lb-variants img {
  width: 56px; height: 56px; object-fit: contain; background: var(--surface-2);
  border: 2px solid var(--line); border-radius: 8px; cursor: pointer; padding: 3px;
}
.lb-variants img.active { border-color: var(--primary); }
.lb-actions { margin-top: auto; display: flex; gap: 10px; }
.lb-nav {
  position: absolute; top: 50%; transform: translateY(-50%);
  width: 48px; height: 48px; border-radius: 50%; border: none;
  background: var(--surface); color: var(--text); box-shadow: var(--shadow-md); font-size: 22px;
  display: flex; align-items: center; justify-content: center;
}
.lb-prev { left: 24px; } .lb-next { right: 24px; }
.lb-close { position: absolute; top: 20px; right: 24px; width: 44px; height: 44px; border-radius: 50%;
  background: var(--surface); border: none; box-shadow: var(--shadow-md); font-size: 22px; }

/* ───────────────────────── Diaporama ───────────────────────── */
.slides {
  position: fixed; inset: 0; z-index: 70; background: var(--bg);
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 22px; padding: 4vh 4vw;
}
.slides img { max-width: 82vw; max-height: 62vh; object-fit: contain; }
.slides .s-label { font-size: clamp(34px, 6vw, 72px); font-weight: 800; text-align: center; }
.slides .s-ipa { font-size: clamp(16px, 2.4vw, 28px); color: var(--muted); margin-top: -10px; }
.slides .s-counter { color: var(--muted); font-size: 15px; }
.slides .s-bar { position: absolute; top: 0; left: 0; height: 4px; background: var(--primary); transition: width .2s; }
.slides .s-controls { position: absolute; bottom: 4vh; display: flex; gap: 12px; align-items: center; }
.slides .s-help { position: absolute; top: 18px; left: 0; right: 0; text-align: center; color: var(--faint); font-size: 13px; }
.slides .s-close { position: absolute; top: 16px; right: 20px; }

/* ───────────────────────── Responsive ───────────────────────── */
@media (max-width: 860px) {
  .brand .tagline { display: none; }
  .basket-btn-label { display: none; }
  .searchbar { order: 3; flex-basis: 100%; max-width: none; margin: 0; }
  .topbar { flex-wrap: wrap; gap: 10px; padding-bottom: 12px; }
  .filters-toggle { display: flex; }
  .layout { grid-template-columns: 1fr; padding: 16px; gap: 16px; }
  .filters {
    position: fixed; top: 0; left: 0; bottom: 0; width: min(330px, 86vw); z-index: 50;
    border-radius: 0; max-height: 100vh; transform: translateX(-100%); transition: transform .25s ease;
    box-shadow: var(--shadow-lg);
  }
  .filters.open { transform: translateX(0); }
  .filters-close { display: inline-flex; }
  .filters-backdrop { position: fixed; inset: 0; background: rgba(10,12,22,.45); z-index: 45; }
  .lb-card { grid-template-columns: 1fr; }
  .lb-imgcol { padding: 18px; }
  .lb-info { padding: 20px; }
  .selbar { flex-direction: column; gap: 10px; border-radius: var(--radius); padding: 12px 16px; width: calc(100vw - 24px); }
  .selbar-actions { width: 100%; justify-content: center; }
}
@media (max-width: 520px) {
  .grid { grid-template-columns: repeat(auto-fill, minmax(130px, 1fr)); gap: 10px; }
  .results-tools .size-control { display: none; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: .01ms !important; transition-duration: .01ms !important; }
}

/* ───────────────────────── Print ───────────────────────── */
@media print {
  body { background: #fff; }
  .topbar, .filters, .filters-toggle, .active-filters, .results-header,
  .selbar, .sentinel, .grid-status, .zoom-btn, .variant-btn, .check { display: none !important; }
  .layout { display: block; padding: 0; }
  .grid { grid-template-columns: repeat(var(--print-cols, 3), 1fr); gap: 12px; }
  .card { break-inside: avoid; border: 1px solid #ccc !important; box-shadow: none !important; background: #fff !important; }
}
