/* Base: reset, type, browser surfaces, shell layout. */

*, *::before, *::after { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--paper);
  color: var(--ink);
  font-family: var(--font-ui);
  font-size: var(--text-md);
  line-height: 1.6;
  -webkit-font-smoothing: antialiased;
  font-feature-settings: "kern";
}
img, svg, video, audio { display: block; max-width: 100%; }
button, input, select, textarea { font: inherit; color: inherit; }
[hidden], [x-cloak] { display: none !important; }

/* Browser surfaces */
::selection { background: var(--selection); }
input, textarea { caret-color: var(--accent); }
* { scrollbar-width: thin; scrollbar-color: var(--rule-strong) transparent; }
::-webkit-scrollbar { width: 10px; height: 10px; }
::-webkit-scrollbar-thumb { background: var(--rule-strong); border-radius: 10px; border: 3px solid var(--paper); }
::-webkit-scrollbar-track { background: transparent; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; border-radius: var(--radius-sm); }
a { color: inherit; text-decoration-color: var(--rule-strong); text-underline-offset: 3px; text-decoration-thickness: 1px; }
a:hover { text-decoration-color: currentColor; }
.tnum, table { font-variant-numeric: tabular-nums; }

/* Type */
h1, h2, h3, h4 { margin: 0; line-height: 1.25; font-weight: 600; letter-spacing: -0.01em; text-wrap: balance; }
h1 { font-size: var(--text-2xl); }
h2 { font-size: var(--text-xl); }
h3 { font-size: var(--text-lg); }
p { margin: 0 0 var(--s-3); text-wrap: pretty; }
.read { font-family: var(--font-read); line-height: 1.7; max-width: 68ch; }
.ipa { font-family: var(--font-ipa); font-weight: 400; letter-spacing: 0; }
.muted { color: var(--ink-muted); }
.faint { color: var(--ink-faint); }
.small { font-size: var(--text-sm); }
.xs { font-size: var(--text-xs); }
.icon { flex: none; stroke: currentColor; fill: none; stroke-width: 1.75; stroke-linecap: round; stroke-linejoin: round; }
.sr-only { position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; border: 0; }
.skip-link { position: absolute; left: var(--s-3); top: -60px; z-index: 100; background: var(--paper); padding: var(--s-2) var(--s-3); border-radius: var(--radius); box-shadow: var(--shadow-pop); }
.skip-link:focus { top: var(--s-3); }

/* ---------- App shell ---------- */
.shell { display: grid; grid-template-columns: var(--sidebar-w) minmax(0, 1fr); min-height: 100dvh; }
.shell[data-collapsed="true"] { grid-template-columns: 0 minmax(0, 1fr); }

.sidebar {
  position: sticky; top: 0; height: 100dvh; overflow: hidden auto;
  background: var(--wash);
  border-right: 1px solid var(--rule);
  display: flex; flex-direction: column; gap: var(--s-1);
  padding: var(--s-3) var(--s-2);
}
.shell[data-collapsed="true"] .sidebar { visibility: hidden; padding: 0; border: 0; }

.brand { display: flex; align-items: center; gap: var(--s-2); padding: var(--s-1) var(--s-2) var(--s-3); text-decoration: none; }
.brand__mark {
  font-family: var(--font-read); font-weight: 650; font-size: 1.375rem; line-height: 1; letter-spacing: -0.02em;
}
.brand__mark span { color: var(--accent); }
.brand__user { margin-left: auto; font-size: var(--text-xs); color: var(--ink-muted); max-width: 9rem; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

/* Quick AI lookup (sidebar and the mobile "Thêm" sheet) */
.quick-lookup {
  position: relative; display: flex; align-items: center; gap: var(--s-2);
  margin: 0 0 var(--s-2); padding: 0 var(--s-2); min-height: 34px;
  border: 1px solid var(--rule); border-radius: var(--radius); background: var(--paper); color: var(--ink-muted);
}
.quick-lookup:focus-within { border-color: var(--accent); color: var(--accent); }
.quick-lookup__input { flex: 1; min-width: 0; height: 32px; border: 0; padding: 0; background: none; color: var(--ink); font: inherit; font-size: var(--text-sm); outline: none; }
.quick-lookup__input::placeholder { color: var(--ink-placeholder); }
.quick-lookup__input::-webkit-search-cancel-button { display: none; }
.quick-lookup__kbd { flex: none; padding: 1px 6px; border: 1px solid var(--rule); border-radius: var(--radius-sm); color: var(--ink-muted); font: 500 var(--text-xs)/1.4 var(--font-ui); }
.quick-lookup:focus-within .quick-lookup__kbd { visibility: hidden; }
.sheet .quick-lookup { margin: 0 0 var(--s-3); min-height: 44px; }
.sheet .quick-lookup__input { height: 42px; font-size: var(--text-md); }

.nav { display: flex; flex-direction: column; gap: 1px; }
.nav__item {
  display: flex; align-items: center; gap: var(--s-2);
  min-height: 32px; padding: 0 var(--s-2);
  border-radius: var(--radius);
  color: var(--ink-muted); text-decoration: none; font-size: var(--text-sm); font-weight: 500;
}
.nav__item:hover { background: var(--wash-strong); color: var(--ink); }
.nav__item--button { width: 100%; border: 0; background: none; cursor: pointer; text-align: left; }
.nav__item[aria-current="page"] { background: var(--wash-strong); color: var(--ink); }
.nav__item[aria-current="page"] .icon { color: var(--accent); }
.nav__count { margin-left: auto; font-size: var(--text-xs); color: var(--accent); font-variant-numeric: tabular-nums; font-weight: 600; }
.sidebar__foot { margin-top: auto; display: flex; flex-direction: column; gap: 1px; padding-top: var(--s-3); border-top: 1px solid var(--rule); }

.main { min-width: 0; display: flex; flex-direction: column; }
.topbar {
  position: sticky; top: 0; z-index: 20;
  height: var(--topbar-h);
  display: flex; align-items: center; gap: var(--s-2);
  padding: 0 var(--s-3);
  background: color-mix(in srgb, var(--paper) 92%, transparent);
  backdrop-filter: saturate(1.4) blur(8px);
}
/* Crumbs give way to the actions: each one shrinks to an ellipsis, the actions never shrink. */
.crumbs { flex: 1 1 auto; display: flex; align-items: center; gap: var(--s-1); min-width: 0; overflow: hidden; font-size: var(--text-sm); color: var(--ink-muted); }
.crumbs > :not(.icon) { flex: 0 1 auto; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.crumbs a { text-decoration: none; padding: 2px 6px; border-radius: var(--radius-sm); }
.crumbs a:hover { background: var(--wash); color: var(--ink); }
/* The current page is short ("Sửa", a date) and is the one worth reading: the links give way first. */
.crumbs [aria-current] { flex: none; color: var(--ink); padding: 2px 6px; }
.topbar__actions { flex: none; margin-left: auto; display: flex; align-items: center; gap: var(--s-2); }

.page { width: 100%; max-width: var(--page-w); margin: 0 auto; padding: var(--s-6) var(--s-6) var(--s-8); }
.page--wide { max-width: 1180px; }
.page--full { max-width: none; }
.page__head { margin-bottom: var(--s-5); }
.page__title { font-size: var(--text-3xl); font-weight: 650; letter-spacing: -0.02em; line-height: 1.15; }
.page__lede { margin-top: var(--s-2); color: var(--ink-muted); font-size: var(--text-lg); max-width: 60ch; }
.section { margin-top: var(--s-7); }
.section__head { display: flex; align-items: baseline; gap: var(--s-3); margin-bottom: var(--s-3); }
.section__head h2 { font-size: var(--text-lg); }
.section__head .actions { margin-left: auto; }

/* Mobile tab bar */
.tabbar { display: none; }

@media (max-width: 767px) {
  .shell, .shell[data-collapsed="true"] { grid-template-columns: minmax(0, 1fr); }
  .sidebar {
    position: fixed; inset: 0 auto 0 0; z-index: 60; width: min(84vw, 300px);
    transform: translateX(-102%); transition: transform 260ms var(--ease-out);
    box-shadow: var(--shadow-pop);
  }
  .shell[data-drawer="open"] .sidebar { transform: none; visibility: visible; }
  .drawer-scrim { position: fixed; inset: 0; z-index: 55; background: rgba(0, 0, 0, 0.32); }
  .page { padding: var(--s-4) var(--s-4) calc(var(--tabbar-h) + var(--s-7)); }
  .page__title { font-size: var(--text-2xl); }
  /* A phone keeps only the last step back and the current page. */
  .crumbs > :not(:nth-last-child(-n+3)) { display: none; }
  .tabbar {
    display: grid; grid-auto-flow: column; grid-auto-columns: 1fr;
    position: fixed; inset: auto 0 0 0; z-index: 40;
    height: calc(var(--tabbar-h) + env(safe-area-inset-bottom));
    padding-bottom: env(safe-area-inset-bottom);
    background: color-mix(in srgb, var(--paper) 94%, transparent);
    backdrop-filter: saturate(1.4) blur(10px);
    border-top: 1px solid var(--rule);
  }
  .tabbar__item {
    display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px;
    min-height: 44px; border: 0; background: none; text-decoration: none;
    color: var(--ink-muted); font-size: 11px; font-weight: 500;
  }
  .tabbar__item[aria-current="page"] { color: var(--accent); }
}

@media (min-width: 768px) {
  .only-mobile { display: none !important; }
}
@media (max-width: 767px) {
  .only-desktop { display: none !important; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: 0.01ms !important; animation-iteration-count: 1 !important; transition-duration: 0.01ms !important; scroll-behavior: auto !important; }
}
