/* Trợ lý lâm sàng — clinical workspace. Tokens and rules: see DESIGN.md. */

:root {
  color-scheme: light dark;

  --canvas: #f4f6f6;
  --surface: #ffffff;
  --surface-subtle: #f1f4f3;
  --surface-sunken: #e8edec;
  --ink: #111d1a;
  --ink-2: #3c4b47;
  --ink-3: #687874;
  --line: #dfe5e3;
  --line-strong: #c9d3d0;

  --accent: #0b6e63;
  --accent-hover: #095a51;
  --accent-ink: #ffffff;
  --accent-soft: #e0f0ec;
  --accent-tint: #f2f8f6;
  --accent-text: #0a6157;
  --tile-from: #0d7a6e;
  --tile-to: #08524a;

  --warn: #8a5a00;
  --warn-soft: #fdf4e3;
  --warn-line: #f1dcae;
  --danger: #b42318;
  --danger-soft: #fdeceb;

  --shadow-sm: 0 1px 2px rgba(17, 29, 26, 0.05);
  --shadow-md: 0 1px 2px rgba(17, 29, 26, 0.04), 0 10px 28px -10px rgba(17, 29, 26, 0.16);
  --focus: 0 0 0 3px rgba(11, 110, 99, 0.28);

  --radius-sm: 8px;
  --radius: 12px;
  --radius-lg: 16px;
  --topbar-height: 60px;
  --nav-width: 248px;
  --gutter: 32px;

  font-family: ui-sans-serif, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
}

@media (prefers-color-scheme: dark) {
  :root {
    --canvas: #0d1412;
    --surface: #141d1a;
    --surface-subtle: #19241f;
    --surface-sunken: #0f1715;
    --ink: #e9f1ee;
    --ink-2: #b3c2bd;
    --ink-3: #82938d;
    --line: #25322e;
    --line-strong: #34443f;

    --accent: #4fc4a9;
    --accent-hover: #6fd3bc;
    --accent-ink: #06231d;
    --accent-soft: #16352e;
    --accent-tint: #111f1b;
    --accent-text: #72d6bf;
    --tile-from: #145e54;
    --tile-to: #0c3f38;

    --warn: #f0c26a;
    --warn-soft: #2a2213;
    --warn-line: #4a3b1c;
    --danger: #ff9e92;
    --danger-soft: #3a1d1a;

    --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.3);
    --shadow-md: 0 1px 2px rgba(0, 0, 0, 0.3), 0 12px 32px -12px rgba(0, 0, 0, 0.6);
    --focus: 0 0 0 3px rgba(79, 196, 169, 0.35);
  }
}

/* ---------- Base ---------- */

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

[hidden] {
  display: none !important;
}

html {
  min-width: 320px;
  background: var(--canvas);
  -webkit-text-size-adjust: 100%;
}

body {
  margin: 0;
  min-height: 100dvh;
  color: var(--ink);
  background: var(--canvas);
  font-size: 16px;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

button,
textarea,
input {
  font: inherit;
  color: inherit;
}

button {
  -webkit-tap-highlight-color: transparent;
}

button:focus-visible,
textarea:focus-visible,
input:focus-visible,
a:focus-visible {
  outline: none;
  box-shadow: var(--focus);
}

button:disabled {
  cursor: not-allowed;
  opacity: 0.45;
}

::selection {
  color: var(--accent-ink);
  background: var(--accent);
}

* {
  scrollbar-color: var(--line-strong) transparent;
  scrollbar-width: thin;
}

.icon-sprite {
  position: absolute;
  width: 0;
  height: 0;
  overflow: hidden;
}

.icon {
  flex: none;
  width: 20px;
  height: 20px;
  fill: none;
  stroke: currentColor;
  stroke-width: 1.8;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  margin: -1px;
  padding: 0;
  overflow: hidden;
  clip: rect(0 0 0 0);
  white-space: nowrap;
  border: 0;
}

.skip-link {
  position: fixed;
  top: 10px;
  left: 10px;
  z-index: 50;
  padding: 10px 14px;
  color: var(--accent-ink);
  background: var(--accent);
  border-radius: var(--radius-sm);
  font-weight: 650;
  text-decoration: none;
  transform: translateY(-160%);
}

.skip-link:focus {
  transform: translateY(0);
}

.view h1[tabindex="-1"]:focus {
  outline: none;
}

@media (prefers-reduced-motion: no-preference) {
  .view {
    animation: view-in 200ms cubic-bezier(0.2, 0.8, 0.2, 1);
  }

  @keyframes view-in {
    from { opacity: 0; transform: translateY(6px); }
    to { opacity: 1; transform: none; }
  }
}

/* ---------- Buttons ---------- */

.primary-button,
.secondary-button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 9px;
  min-height: 48px;
  padding: 7px 16px;
  border-radius: 10px;
  font-size: 15px;
  font-weight: 650;
  line-height: 1.2;
  cursor: pointer;
  transition: background-color 150ms ease, border-color 150ms ease, color 150ms ease, box-shadow 150ms ease;
}

.primary-button {
  color: var(--accent-ink);
  border: 1px solid var(--accent);
  background: var(--accent);
  box-shadow: var(--shadow-sm);
}

.primary-button:hover:not(:disabled) {
  border-color: var(--accent-hover);
  background: var(--accent-hover);
}

.secondary-button {
  color: var(--ink);
  border: 1px solid var(--line-strong);
  background: var(--surface);
}

.secondary-button:hover:not(:disabled) {
  color: var(--accent-text);
  border-color: var(--accent);
}

.ghost-button {
  display: inline-flex;
  align-items: center;
  gap: 7px;
  min-height: 40px;
  padding: 4px 10px;
  color: var(--accent-text);
  border: 0;
  border-radius: var(--radius-sm);
  background: transparent;
  font-size: 14px;
  font-weight: 620;
  cursor: pointer;
  transition: background-color 150ms ease;
}

.ghost-button:hover:not(:disabled) {
  background: var(--accent-soft);
}

.primary-button .icon,
.secondary-button .icon,
.ghost-button .icon {
  width: 18px;
  height: 18px;
}

/* Bilingual pattern: Vietnamese first, Russian under it, smaller and lighter. */
.button-label,
.bi {
  display: grid;
  justify-items: start;
  text-align: left;
}

.button-label {
  line-height: 1.15;
}

.button-label > span {
  white-space: nowrap;
}

.button-label > [lang="ru"] {
  font-size: 11.5px;
  font-weight: 500;
  opacity: 0.82;
}

.ghost-button .button-label > [lang="ru"] {
  font-size: 11px;
}

.bi > [lang="ru"] {
  color: var(--ink-3);
  font-size: 0.8em;
  font-weight: 520;
}

.page-title-ru {
  margin: 3px 0 0;
  color: var(--accent-text);
  font-size: clamp(16px, 1.6vw, 19px);
  font-weight: 620;
}

button:active:not(:disabled) {
  transform: translateY(1px);
}

/* ---------- Top bar ---------- */

.topbar {
  position: sticky;
  top: 0;
  z-index: 30;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  height: calc(var(--topbar-height) + env(safe-area-inset-top));
  padding: env(safe-area-inset-top) max(20px, env(safe-area-inset-right)) 0 max(20px, env(safe-area-inset-left));
  border-bottom: 1px solid var(--line);
  background: color-mix(in srgb, var(--canvas) 86%, transparent);
  -webkit-backdrop-filter: saturate(1.4) blur(14px);
  backdrop-filter: saturate(1.4) blur(14px);
}

.brand-button {
  display: flex;
  align-items: center;
  gap: 11px;
  min-width: 0;
  padding: 4px 6px 4px 0;
  border: 0;
  border-radius: var(--radius-sm);
  background: transparent;
  text-align: left;
  cursor: pointer;
}

.brand-mark {
  display: grid;
  place-items: center;
  flex: none;
  width: 34px;
  height: 34px;
  color: #ffffff;
  border-radius: 9px;
  background: linear-gradient(160deg, var(--tile-from), var(--tile-to));
  box-shadow: var(--shadow-sm);
}

.brand-mark .icon {
  width: 19px;
  height: 19px;
  stroke-width: 2;
}

.brand-copy {
  display: grid;
  min-width: 0;
  line-height: 1.2;
}

.brand-copy strong {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-size: 15px;
  font-weight: 700;
  letter-spacing: -0.01em;
}

.brand-copy small {
  overflow: hidden;
  color: var(--ink-3);
  font-size: 12px;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.service-state {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  min-width: 0;
  padding: 6px 12px;
  color: var(--ink-2);
  border: 1px solid var(--line);
  border-radius: 999px;
  background: var(--surface);
  font-size: 13px;
  font-weight: 560;
  white-space: nowrap;
}

#service-label {
  overflow: hidden;
  text-overflow: ellipsis;
}

.state-short {
  display: none;
}

.state-indicator {
  flex: none;
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-soft);
}

.service-state[data-state="locked"] .state-indicator {
  background: var(--warn);
  box-shadow: 0 0 0 3px var(--warn-soft);
}

.service-state[data-state="offline"] .state-indicator {
  background: var(--danger);
  box-shadow: 0 0 0 3px var(--danger-soft);
}

/* ---------- App shell and navigation ---------- */

.app-body {
  min-height: calc(100dvh - var(--topbar-height));
}

main {
  min-width: 0;
}

.app-nav {
  position: fixed;
  right: 0;
  bottom: 0;
  left: 0;
  z-index: 30;
  display: flex;
  gap: 2px;
  padding: 6px max(8px, env(safe-area-inset-right)) calc(6px + env(safe-area-inset-bottom)) max(8px, env(safe-area-inset-left));
  border-top: 1px solid var(--line);
  background: color-mix(in srgb, var(--surface) 92%, transparent);
  -webkit-backdrop-filter: saturate(1.4) blur(14px);
  backdrop-filter: saturate(1.4) blur(14px);
}

.nav-item {
  display: flex;
  flex: 1;
  flex-direction: column;
  align-items: center;
  gap: 3px;
  min-width: 0;
  padding: 6px 4px;
  color: var(--ink-3);
  border: 0;
  border-radius: 10px;
  background: transparent;
  font-size: 11.5px;
  font-weight: 620;
  cursor: pointer;
  transition: color 150ms ease, background-color 150ms ease;
}

.nav-item .icon {
  width: 22px;
  height: 22px;
}

.nav-copy {
  display: grid;
  min-width: 0;
  text-align: center;
}

.nav-copy > span {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.nav-copy small {
  overflow: hidden;
  font-size: 10px;
  font-weight: 500;
  line-height: 1.2;
  text-overflow: ellipsis;
  white-space: nowrap;
  opacity: 0.85;
}

.nav-note {
  display: none;
}

.nav-item[aria-current="page"] {
  color: var(--accent-text);
}

main {
  padding-bottom: calc(88px + env(safe-area-inset-bottom));
}

body.is-locked .app-nav {
  display: none;
}

body.is-locked main {
  padding-bottom: 0;
}

@media (min-width: 1024px) {
  .app-body {
    display: grid;
    grid-template-columns: var(--nav-width) minmax(0, 1fr);
  }

  body.is-locked .app-body {
    grid-template-columns: minmax(0, 1fr);
  }

  .app-nav {
    position: sticky;
    top: var(--topbar-height);
    right: auto;
    bottom: auto;
    left: auto;
    flex-direction: column;
    gap: 4px;
    height: calc(100dvh - var(--topbar-height));
    padding: 20px 14px;
    border-top: 0;
    border-right: 1px solid var(--line);
    background: transparent;
    -webkit-backdrop-filter: none;
    backdrop-filter: none;
  }

  .nav-item {
    flex: none;
    flex-direction: row;
    gap: 12px;
    padding: 9px 12px;
    color: var(--ink-2);
    font-size: 15px;
    text-align: left;
  }

  .nav-item .icon {
    width: 20px;
    height: 20px;
  }

  .nav-copy {
    text-align: left;
    line-height: 1.25;
  }

  .nav-copy small {
    color: var(--ink-3);
    font-size: 12px;
    opacity: 1;
  }

  .nav-item:hover {
    background: var(--surface-subtle);
  }

  .nav-item[aria-current="page"] {
    color: var(--accent-text);
    background: var(--accent-soft);
  }

  .nav-item[aria-current="page"] .nav-copy small {
    color: var(--accent-text);
    opacity: 0.8;
  }

  .nav-note {
    display: flex;
    gap: 10px;
    margin: auto 0 0;
    padding: 12px;
    color: var(--ink-3);
    border-radius: var(--radius);
    background: var(--surface-subtle);
    font-size: 12.5px;
    line-height: 1.4;
  }

  .nav-note .icon {
    width: 18px;
    height: 18px;
    color: var(--accent-text);
  }

  .nav-note span[lang="ru"] {
    display: block;
    margin-top: 2px;
  }

  main {
    padding-bottom: 0;
  }
}

/* ---------- Page scaffolding ---------- */

.page {
  container: page / inline-size;
  width: min(1120px, calc(100% - 2 * var(--gutter)));
  margin: 0 auto;
  padding: 32px 0 56px;
}

.home-view {
  width: min(1040px, calc(100% - 2 * var(--gutter)));
}

.reading-view {
  width: min(1260px, calc(100% - 2 * var(--gutter)));
}

.page-header {
  margin-bottom: 24px;
}

.page-header-split {
  display: flex;
  flex-wrap: wrap;
  align-items: flex-end;
  justify-content: space-between;
  gap: 20px 32px;
}

.eyebrow {
  margin: 0;
  color: var(--accent-text);
  font-size: 12.5px;
  font-weight: 700;
  letter-spacing: 0.05em;
  text-transform: uppercase;
}

.page-header h1 {
  margin: 6px 0 0;
  font-size: clamp(25px, 2.6vw, 32px);
  font-weight: 720;
  line-height: 1.15;
  letter-spacing: -0.022em;
}

.page-subtitle {
  margin: 6px 0 0;
  color: var(--ink-2);
  font-size: 15px;
}

.page-subtitle span[lang="ru"] {
  color: var(--ink-3);
}

.notice {
  display: flex;
  gap: 12px;
  padding: 14px 16px;
  color: var(--ink-2);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--surface-subtle);
  font-size: 14px;
  line-height: 1.45;
}

.notice .icon {
  color: var(--accent-text);
}

.notice strong {
  display: block;
  color: var(--ink);
}

.notice-safety {
  margin-top: 20px;
}

/* ---------- Home ---------- */

.tile-grid {
  display: grid;
  gap: 12px;
}

@container page (min-width: 760px) {
  .tile-grid {
    grid-template-columns: repeat(3, minmax(0, 1fr));
    gap: 16px;
  }
}

.tile {
  display: flex;
  align-items: center;
  gap: 14px;
  padding: 18px;
  color: var(--ink);
  border: 1px solid var(--line);
  border-radius: var(--radius-lg);
  background: var(--surface);
  box-shadow: var(--shadow-sm);
  text-align: left;
  cursor: pointer;
  transition: border-color 160ms ease, box-shadow 160ms ease, transform 160ms ease;
}

.tile:hover {
  border-color: var(--line-strong);
  box-shadow: var(--shadow-md);
}

@container page (min-width: 760px) {
  .tile {
    flex-direction: column;
    align-items: stretch;
    gap: 18px;
    min-height: 236px;
    padding: 22px;
  }

  .tile:hover {
    transform: translateY(-2px);
  }
}

.tile-icon {
  display: grid;
  place-items: center;
  flex: none;
  width: 44px;
  height: 44px;
  color: var(--accent-text);
  border-radius: var(--radius);
  background: var(--accent-soft);
}

.tile-icon .icon {
  width: 22px;
  height: 22px;
}

.tile-copy {
  display: grid;
  flex: 1;
  gap: 2px;
  min-width: 0;
}

.tile-copy strong {
  font-size: 18px;
  font-weight: 720;
  line-height: 1.25;
  letter-spacing: -0.01em;
}

/* Title block (Vietnamese + Russian) stands apart from the description. */
.tile-copy > span {
  color: var(--accent-text);
  font-size: 14.5px;
  font-weight: 620;
}

.tile-copy small {
  margin-top: 12px;
  padding-top: 11px;
  color: var(--ink-3);
  border-top: 1px solid var(--line);
  font-size: 13px;
  line-height: 1.45;
}

.tile-copy small [lang="ru"] {
  display: block;
  margin-top: 2px;
}

.tile-action {
  display: none;
}

@container page (min-width: 760px) {
  .tile-copy strong {
    font-size: 18px;
  }

  .tile-action {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    margin-top: auto;
    color: var(--accent-text);
    font-size: 14px;
    font-weight: 650;
  }

  .tile-action .icon {
    width: 16px;
    height: 16px;
    transition: transform 160ms ease;
  }

  .tile:hover .tile-action .icon {
    transform: translateX(3px);
  }
}

.tile-primary {
  color: #ffffff;
  border-color: transparent;
  background: linear-gradient(155deg, var(--tile-from), var(--tile-to));
  box-shadow: var(--shadow-md);
}

.tile-primary:hover {
  border-color: transparent;
}

.tile-primary .tile-icon {
  color: #ffffff;
  background: rgba(255, 255, 255, 0.14);
}

.tile-primary .tile-copy > span {
  color: #ffffff;
  opacity: 0.92;
}

.tile-primary .tile-copy small {
  color: rgba(255, 255, 255, 0.72);
  border-top-color: rgba(255, 255, 255, 0.2);
}

.tile-primary .tile-action {
  color: #ffffff;
}

/* ---------- Translator ---------- */

.direction-switch {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 4px;
  width: min(540px, 100%);
  padding: 4px;
  border: 1px solid var(--line);
  border-radius: 14px;
  background: var(--surface-sunken);
}

.direction-option {
  display: flex;
  align-items: center;
  gap: 10px;
  min-width: 0;
  min-height: 52px;
  padding: 8px 12px;
  color: var(--ink-2);
  border: 0;
  border-radius: 10px;
  background: transparent;
  text-align: left;
  cursor: pointer;
  transition: background-color 150ms ease, color 150ms ease, box-shadow 150ms ease;
}

.direction-option:hover:not(.is-active) {
  color: var(--ink);
}

.direction-option.is-active {
  color: var(--ink);
  background: var(--surface);
  box-shadow: var(--shadow-sm), 0 0 0 1px var(--line);
}

.language-code {
  display: flex;
  flex: none;
}

.country-flag {
  display: block;
  width: 26px;
  height: 18px;
  border-radius: 3px;
  box-shadow: 0 0 0 1px rgba(0, 0, 0, 0.1);
}

.direction-copy {
  display: grid;
  min-width: 0;
  line-height: 1.25;
}

.direction-copy strong {
  font-size: 14.5px;
  font-weight: 680;
}

.direction-copy small {
  overflow: hidden;
  color: var(--ink-3);
  font-size: 12px;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.translation-bench {
  display: grid;
  overflow: hidden;
  border: 1px solid var(--line);
  border-radius: var(--radius-lg);
  background: var(--surface);
  box-shadow: var(--shadow-md);
}

@container page (min-width: 720px) {
  .translation-bench {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }
}

.input-side,
.output-side {
  display: flex;
  flex-direction: column;
  gap: 14px;
  min-width: 0;
  padding: 18px 20px 20px;
}

.input-side {
  border-bottom: 1px solid var(--line);
  transition: box-shadow 150ms ease;
}

.input-side:focus-within {
  box-shadow: inset 0 0 0 2px color-mix(in srgb, var(--accent) 30%, transparent);
}

.output-side {
  min-height: 240px;
  background: var(--accent-tint);
  scroll-margin-top: calc(var(--topbar-height) + 12px);
}

@container page (min-width: 720px) {
  .input-side,
  .output-side {
    min-height: 380px;
    padding: 20px 24px 22px;
  }

  .input-side {
    border-right: 1px solid var(--line);
    border-bottom: 0;
  }
}

.panel-heading {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 12px;
}

.panel-heading h2 {
  margin: 2px 0 0;
  font-size: 16.5px;
  font-weight: 660;
  letter-spacing: -0.01em;
}

.panel-label {
  margin: 0;
  color: var(--accent-text);
  font-size: 11.5px;
  font-weight: 720;
  letter-spacing: 0.06em;
  text-transform: uppercase;
}

#translate-form textarea {
  flex: 1;
  width: 100%;
  min-height: 112px;
  padding: 0;
  color: var(--ink);
  border: 0;
  background: transparent;
  font-size: 20px;
  line-height: 1.45;
  caret-color: var(--accent);
  resize: none;
}

#translate-form textarea:focus-visible {
  box-shadow: none;
}

#translate-form textarea::placeholder {
  color: color-mix(in srgb, var(--ink-3) 75%, transparent);
}

@container page (min-width: 720px) {
  #translate-form textarea {
    min-height: 200px;
    font-size: 22px;
  }
}

.input-actions,
.output-actions {
  display: flex;
  gap: 10px;
  margin-top: auto;
}

.input-actions {
  justify-content: space-between;
}

.output-actions {
  justify-content: flex-end;
}

@container page (max-width: 519px) {
  .input-actions .primary-button,
  .output-actions .primary-button {
    flex: 1;
  }
}

.input-actions .primary-button,
.output-actions .primary-button {
  white-space: nowrap;
}

.supporting-text {
  margin: -4px 0 0;
  color: var(--ink-3);
  font-size: 13px;
}

.supporting-text:empty {
  display: none;
}

.empty-output {
  display: grid;
  flex: 1;
  place-content: center;
  justify-items: center;
  gap: 4px;
  padding: 24px 12px;
  color: var(--ink-3);
  text-align: center;
}

.empty-output .icon {
  width: 30px;
  height: 30px;
  margin-bottom: 6px;
  color: var(--line-strong);
}

.empty-output p {
  margin: 0;
  color: var(--ink-2);
  font-size: 15.5px;
  font-weight: 600;
}

.empty-output span {
  font-size: 13px;
}

.loading-output {
  display: grid;
  flex: 1;
  align-content: center;
  gap: 12px;
}

.loading-output span {
  height: 22px;
  border-radius: 7px;
  background: color-mix(in srgb, var(--accent) 12%, var(--surface));
}

.loading-output span:nth-child(1) { width: 92%; }
.loading-output span:nth-child(2) { width: 76%; }
.loading-output span:nth-child(3) { width: 48%; }

@media (prefers-reduced-motion: no-preference) {
  .loading-output span {
    animation: loading-pulse 1.1s ease-in-out infinite alternate;
  }

  .loading-output span:nth-child(2) { animation-delay: 120ms; }
  .loading-output span:nth-child(3) { animation-delay: 240ms; }

  @keyframes loading-pulse {
    from { opacity: 0.45; }
    to { opacity: 1; }
  }
}

.result-output {
  flex: 1;
}

.translation-text {
  margin: 0;
  color: var(--ink);
  font-size: clamp(25px, 2.5vw, 32px);
  font-weight: 650;
  line-height: 1.3;
  letter-spacing: -0.015em;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}

.translation-note {
  margin-top: 16px;
  padding: 11px 14px;
  color: var(--ink-2);
  border: 1px solid var(--line);
  border-left: 3px solid var(--accent);
  border-radius: var(--radius-sm);
  background: var(--surface);
  font-size: 14.5px;
  line-height: 1.45;
  white-space: pre-wrap;
}

.translation-source {
  margin: 12px 0 0;
  color: var(--ink-3);
  font-size: 12.5px;
}

.translation-source:empty {
  display: none;
}

.error-output {
  padding: 12px 14px;
  color: var(--danger);
  border-radius: var(--radius-sm);
  background: var(--danger-soft);
  font-size: 14.5px;
}

.translator-boundary {
  display: flex;
  align-items: flex-start;
  gap: 10px;
  margin: 14px 4px 0;
  color: var(--ink-3);
  font-size: 13px;
  line-height: 1.45;
}

.translator-boundary .icon {
  width: 17px;
  height: 17px;
  margin-top: 1px;
}

.translator-boundary strong {
  color: var(--ink-2);
  font-weight: 650;
}

.translator-boundary span[lang="ru"] {
  display: block;
}

.conversation-section {
  margin-top: 28px;
}

.conversation-heading {
  display: flex;
  align-items: flex-end;
  justify-content: space-between;
  gap: 12px;
}

.conversation-heading h2 {
  margin: 0;
  font-size: 17px;
  font-weight: 680;
}

.conversation-heading p {
  margin: 2px 0 0;
  color: var(--ink-3);
  font-size: 13px;
}

.conversation-list {
  display: grid;
  gap: 10px;
  margin: 14px 0 0;
  padding: 0;
  list-style: none;
}

.conversation-item {
  display: grid;
  overflow: hidden;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--surface);
}

.conversation-item > div {
  min-width: 0;
  padding: 12px 16px;
}

.conversation-item > div + div {
  border-top: 1px solid var(--line);
  background: var(--accent-tint);
}

@container page (min-width: 720px) {
  .conversation-item {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .conversation-item > div + div {
    border-top: 0;
    border-left: 1px solid var(--line);
  }
}

.conversation-item small {
  color: var(--ink-3);
  font-size: 11.5px;
  font-weight: 700;
  letter-spacing: 0.05em;
  text-transform: uppercase;
}

.conversation-item p {
  margin: 3px 0 0;
  font-size: 15px;
  overflow-wrap: anywhere;
}

.conversation-empty {
  padding: 18px;
  color: var(--ink-3);
  border: 1px dashed var(--line-strong);
  border-radius: var(--radius);
  font-size: 14px;
  text-align: center;
}

/* ---------- Reading views: document + assistant ---------- */

.draft-notice {
  display: flex;
  gap: 12px;
  margin-bottom: 20px;
  padding: 13px 16px;
  color: var(--ink-2);
  border: 1px solid var(--warn-line);
  border-radius: var(--radius);
  background: var(--warn-soft);
  font-size: 14px;
  line-height: 1.45;
}

.draft-notice .icon {
  color: var(--warn);
}

.draft-notice strong {
  display: block;
  color: var(--ink);
}

.draft-notice span {
  display: block;
}

.draft-notice span[lang="ru"] {
  color: var(--ink-3);
  font-size: 13px;
}

.reading-layout {
  display: grid;
  grid-template-areas: "doc" "agent";
  gap: 20px;
  align-items: start;
}

/* On a phone the topics come first; the assistant follows. */
.library-layout {
  grid-template-areas: "index" "doc" "agent";
}

.section-agent { grid-area: agent; }
.knowledge-document { grid-area: doc; }
.library-index { grid-area: index; }

@container page (min-width: 880px) {
  .reading-layout {
    grid-template-areas: "doc agent";
    grid-template-columns: minmax(0, 1fr) 360px;
    gap: 24px;
  }

  .library-layout {
    grid-template-areas: "index index" "doc agent";
    gap: 16px 24px;
  }

  .section-agent {
    position: sticky;
    top: calc(var(--topbar-height) + 20px);
    max-height: calc(100dvh - var(--topbar-height) - 40px);
  }
}

@container page (min-width: 1200px) {
  .library-layout {
    grid-template-areas: "index doc agent";
    grid-template-columns: 220px minmax(0, 1fr) 340px;
    gap: 24px;
  }
}

.library-search {
  display: flex;
  align-items: center;
  gap: 8px;
  width: min(340px, 100%);
  height: 44px;
  padding: 0 12px;
  color: var(--ink-3);
  border: 1px solid var(--line-strong);
  border-radius: 10px;
  background: var(--surface);
}

.library-search:focus-within {
  border-color: var(--accent);
  box-shadow: var(--focus);
}

.library-search .icon {
  width: 18px;
  height: 18px;
}

.library-search input {
  flex: 1;
  min-width: 0;
  height: 100%;
  padding: 0;
  color: var(--ink);
  border: 0;
  background: transparent;
  font-size: 15px;
}

.library-search input:focus-visible {
  box-shadow: none;
}

.library-search input::placeholder {
  color: var(--ink-3);
}

.library-index {
  display: flex;
  gap: 8px;
  min-width: 0;
  overflow-x: auto;
  padding-bottom: 2px;
}

.library-index:empty {
  display: none;
}

.library-index button {
  flex: none;
  max-width: 280px;
  overflow: hidden;
  padding: 7px 13px;
  color: var(--ink-2);
  border: 1px solid var(--line);
  border-radius: 999px;
  background: var(--surface);
  font-size: 13px;
  font-weight: 600;
  text-overflow: ellipsis;
  white-space: nowrap;
  cursor: pointer;
}

.library-index button:hover {
  color: var(--accent-text);
  border-color: var(--accent);
}

@container page (min-width: 1200px) {
  .library-index {
    position: sticky;
    top: calc(var(--topbar-height) + 20px);
    flex-direction: column;
    gap: 2px;
    overflow: visible;
  }

  .library-index::before {
    content: "Mục lục · Содержание";
    margin: 0 0 6px 10px;
    color: var(--ink-3);
    font-size: 11.5px;
    font-weight: 720;
    letter-spacing: 0.06em;
    text-transform: uppercase;
  }

  .library-index button {
    max-width: none;
    padding: 7px 10px;
    border: 0;
    border-radius: var(--radius-sm);
    background: transparent;
    font-size: 13.5px;
    line-height: 1.35;
    text-align: left;
    white-space: normal;
  }

  .library-index button:hover {
    background: var(--surface-subtle);
  }
}

.knowledge-document {
  min-width: 0;
  overflow: hidden;
  border: 1px solid var(--line);
  border-radius: var(--radius-lg);
  background: var(--surface);
  box-shadow: var(--shadow-sm);
}

.knowledge-section {
  padding: 24px 20px;
  border-bottom: 1px solid var(--line);
  scroll-margin-top: calc(var(--topbar-height) + 16px);
}

.knowledge-section:last-child,
.knowledge-section:empty {
  border-bottom: 0;
}

.knowledge-section:empty {
  display: none;
}

@container page (min-width: 720px) {
  .knowledge-section {
    padding: 28px 34px;
  }
}

.knowledge-section h2 {
  max-width: 34ch;
  margin: 0 0 14px;
  font-size: 21px;
  font-weight: 700;
  line-height: 1.25;
  letter-spacing: -0.015em;
}

.knowledge-section h3 {
  margin: 24px 0 8px;
  font-size: 16.5px;
  font-weight: 660;
}

.knowledge-section p,
.knowledge-section li {
  max-width: 72ch;
  line-height: 1.65;
}

.knowledge-section p {
  margin: 0 0 12px;
}

.knowledge-section ul {
  display: grid;
  gap: 8px;
  margin: 8px 0 16px;
  padding-left: 20px;
}

.knowledge-section li::marker {
  color: var(--accent);
}

.document-note {
  padding: 11px 14px;
  color: var(--ink-2);
  border-left: 3px solid var(--line-strong);
  border-radius: 0 var(--radius-sm) var(--radius-sm) 0;
  background: var(--surface-subtle);
  font-size: 14.5px;
}

.content-loading {
  margin: 0;
  padding: 28px;
  color: var(--ink-3);
}

.knowledge-empty {
  display: grid;
  gap: 4px;
  padding: 44px 28px;
  color: var(--ink-3);
  text-align: center;
}

.knowledge-empty p {
  margin: 0;
  color: var(--ink);
  font-size: 17px;
  font-weight: 650;
}

.knowledge-empty span {
  font-size: 14px;
}

/* Topic lists (operations, patient workflow): each title links to its own page. */
.topic-group h2 {
  margin-bottom: 14px;
}

.topic-list {
  display: grid;
  gap: 8px;
  margin: 4px 0 0;
  padding: 0;
  list-style: none;
}

.topic-link {
  display: flex;
  align-items: center;
  gap: 12px;
  min-height: 56px;
  padding: 11px 14px;
  color: var(--ink);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--surface-subtle);
  text-decoration: none;
  transition: border-color 150ms ease, background-color 150ms ease;
}

.topic-link:hover {
  border-color: var(--accent);
  background: var(--accent-tint);
}

.topic-link > .icon {
  margin-left: auto;
  color: var(--accent-text);
  transition: transform 150ms ease;
}

.topic-link:hover > .icon {
  transform: translateX(3px);
}

.topic-title {
  display: grid;
  gap: 1px;
  min-width: 0;
}

.topic-title > span:first-child {
  color: var(--accent-text);
  font-size: 15.5px;
  font-weight: 650;
  line-height: 1.35;
  text-decoration: underline;
  text-decoration-color: color-mix(in srgb, var(--accent) 35%, transparent);
  text-underline-offset: 3px;
}

.topic-title > [lang="ru"] {
  color: var(--ink-2);
  font-size: 13.5px;
  line-height: 1.4;
}

/* One topic per page */
.back-link {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  margin-bottom: 18px;
  color: var(--accent-text);
  font-size: 14px;
  font-weight: 650;
  text-decoration: none;
}

.back-link:hover {
  text-decoration: underline;
}

.icon-back {
  width: 16px;
  height: 16px;
  transform: rotate(180deg);
}

.topic-group-label {
  margin: 0 0 6px;
  color: var(--accent-text);
  font-size: 12.5px;
  font-weight: 700;
  letter-spacing: 0.04em;
  text-transform: uppercase;
}

.knowledge-section .topic-heading {
  display: grid;
  gap: 4px;
  max-width: none;
  margin: 0 0 20px;
  padding-bottom: 18px;
  border-bottom: 1px solid var(--line);
  font-size: clamp(22px, 2.4vw, 28px);
}

.topic-heading > [lang="ru"] {
  color: var(--accent-text);
  font-size: 0.68em;
  font-weight: 620;
  letter-spacing: -0.01em;
}

.topic-body {
  margin-bottom: 20px;
}

.knowledge-section h4 {
  margin: 20px 0 6px;
  font-size: 15px;
  font-weight: 660;
}

.knowledge-section ol {
  display: grid;
  gap: 8px;
  margin: 8px 0 16px;
  padding-left: 22px;
}

.knowledge-section li strong,
.knowledge-section p strong {
  color: var(--ink);
  font-weight: 680;
}

.topic-empty {
  color: var(--ink-3);
}

.topic-pager {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
  gap: 10px;
  margin-top: 24px;
  padding-top: 20px;
  border-top: 1px solid var(--line);
}

.pager-link {
  display: grid;
  gap: 4px;
  padding: 12px 14px;
  color: var(--ink);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  text-decoration: none;
}

.pager-link:hover {
  border-color: var(--accent);
  background: var(--accent-tint);
}

.pager-next {
  text-align: right;
}

.pager-next .topic-title {
  justify-items: end;
}

.pager-link small {
  color: var(--ink-3);
  font-size: 12px;
  font-weight: 600;
}

.library-no-results {
  margin: 0;
  padding: 32px;
  color: var(--ink-3);
  text-align: center;
}

.knowledge-document .error-output {
  margin: 20px;
}

/* ---------- Assistant panel ---------- */

.section-agent {
  display: flex;
  flex-direction: column;
  min-width: 0;
  overflow: hidden;
  border: 1px solid var(--line);
  border-radius: var(--radius-lg);
  background: var(--surface);
  box-shadow: var(--shadow-sm);
}

.agent-heading {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 14px 18px;
  border-bottom: 1px solid var(--line);
}

.agent-icon {
  display: grid;
  place-items: center;
  flex: none;
  width: 36px;
  height: 36px;
  color: var(--accent-text);
  border-radius: 10px;
  background: var(--accent-soft);
}

.agent-heading h2 {
  margin: 0;
  font-size: 15.5px;
  font-weight: 680;
  line-height: 1.3;
}

.agent-heading p {
  margin: 1px 0 0;
  color: var(--ink-3);
  font-size: 12.5px;
}

.agent-thread {
  display: flex;
  flex: 1 1 auto;
  flex-direction: column;
  gap: 10px;
  min-height: 72px;
  max-height: 420px;
  margin: 0;
  padding: 14px 18px;
  overflow-y: auto;
  list-style: none;
}

.agent-empty {
  margin: auto 0;
  color: var(--ink-3);
  font-size: 13.5px;
  text-align: center;
}

.agent-message {
  max-width: 92%;
  padding: 10px 13px;
  font-size: 14.5px;
  line-height: 1.5;
}

.agent-message small {
  display: block;
  margin-bottom: 2px;
  font-size: 11px;
  font-weight: 720;
  letter-spacing: 0.05em;
  text-transform: uppercase;
  opacity: 0.7;
}

.agent-message p {
  margin: 0;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}

.agent-message-user {
  align-self: flex-end;
  color: var(--accent-ink);
  border-radius: 14px 14px 4px 14px;
  background: var(--accent);
}

.agent-message-answer {
  align-self: flex-start;
  color: var(--ink);
  border: 1px solid var(--line);
  border-radius: 14px 14px 14px 4px;
  background: var(--surface-subtle);
}

.agent-form {
  display: grid;
  gap: 8px;
  padding: 14px 18px;
  border-top: 1px solid var(--line);
}

.agent-form > label {
  color: var(--ink-2);
  font-size: 12.5px;
  font-weight: 650;
}

.agent-form textarea {
  width: 100%;
  min-height: 80px;
  padding: 10px 12px;
  color: var(--ink);
  border: 1px solid var(--line-strong);
  border-radius: 10px;
  background: var(--surface);
  font-size: 15px;
  line-height: 1.45;
  resize: vertical;
}

.agent-form textarea:focus-visible {
  border-color: var(--accent);
}

.agent-form textarea::placeholder {
  color: var(--ink-3);
}

.agent-form-actions {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
}

.agent-status {
  margin: 0;
  color: var(--ink-3);
  font-size: 12.5px;
}

.agent-status:empty {
  display: none;
}

.agent-status.is-error {
  color: var(--danger);
}

.agent-privacy {
  margin: 0;
  padding: 0 18px 14px;
  color: var(--ink-3);
  font-size: 12px;
  line-height: 1.4;
}

/* ---------- Login ---------- */

.login-view {
  width: min(440px, calc(100% - 32px));
  margin: 0 auto;
  padding: clamp(36px, 10vh, 96px) 0 48px;
}

.login-card {
  display: grid;
  gap: 18px;
  padding: 28px;
  border: 1px solid var(--line);
  border-radius: 18px;
  background: var(--surface);
  box-shadow: var(--shadow-md);
}

.login-icon {
  display: grid;
  place-items: center;
  width: 44px;
  height: 44px;
  color: var(--accent-text);
  border-radius: var(--radius);
  background: var(--accent-soft);
}

.login-icon .icon {
  width: 22px;
  height: 22px;
}

.login-card h1 {
  margin: 0;
  font-size: 26px;
  font-weight: 720;
  line-height: 1.15;
  letter-spacing: -0.02em;
}

.login-title-ru {
  margin: 4px 0 0;
  color: var(--accent-text);
  font-size: 15px;
  font-weight: 620;
}

.login-lead {
  margin: 0;
  color: var(--ink-2);
  font-size: 14.5px;
}

.login-lead span {
  display: block;
  margin-top: 4px;
  color: var(--ink-3);
  font-size: 13px;
}

.field {
  display: grid;
  gap: 6px;
}

.field label {
  color: var(--ink-2);
  font-size: 12.5px;
  font-weight: 650;
}

.field input {
  width: 100%;
  height: 50px;
  padding: 0 14px;
  color: var(--ink);
  border: 1px solid var(--line-strong);
  border-radius: 10px;
  background: var(--surface);
  font-size: 20px;
  letter-spacing: 0.08em;
}

.field input:focus-visible {
  border-color: var(--accent);
}

.login-card .primary-button {
  min-height: 48px;
}

.login-status {
  margin: -6px 0 0;
  color: var(--danger);
  font-size: 14px;
}

.login-status:empty {
  display: none;
}

/* ---------- Small screens ---------- */

@media (max-width: 640px) {
  :root {
    --gutter: 16px;
    --topbar-height: 56px;
  }

  .topbar {
    padding-right: max(14px, env(safe-area-inset-right));
    padding-left: max(14px, env(safe-area-inset-left));
  }

  .service-state {
    flex: none;
    padding: 5px 10px;
    font-size: 12px;
  }

  .state-long {
    display: none;
  }

  .state-short {
    display: inline;
  }

  .page {
    padding-top: 20px;
  }

  .page-header {
    margin-bottom: 18px;
  }

  .translator-view .page-subtitle {
    display: none;
  }

  .page-title-ru {
    font-size: 16px;
  }

  .direction-switch {
    width: 100%;
  }

  .direction-option {
    gap: 8px;
    padding: 8px 10px;
  }

  .direction-copy small {
    font-size: 11px;
  }

  .translation-text {
    font-size: 25px;
  }

  .library-search {
    width: 100%;
  }

  .agent-form-actions .primary-button {
    flex: 1;
  }

  .login-card {
    padding: 22px;
  }
}
