/* Giao diện ứng dụng — theo .stitch/DESIGN.md */

:root {
  --canvas: #F4F4F5;
  --paper: #FFFFFF;
  --ink: #18181B;
  --ink-2: #52525B;
  --ink-3: #A1A1AA;
  --line: #E4E4E7;
  --wash: #F8FAFC;
  --accent: #2B6CB0;
  --accent-hover: #245A93;
  --accent-tint: #EAF1F8;
  --rose: #BE123C;
  --line-strong: #D4D4D8;
  --primary-bg: #2B6CB0;
  --primary-hover: #245A93;
  --sk-a: #EFEFF1;
  --sk-b: #F7F7F8;
  --shadow: rgba(24, 24, 27, 0.18);
  --backdrop: rgba(24, 24, 27, 0.45);
  --font-sans: 'Be Vietnam Pro', 'Segoe UI', system-ui, sans-serif;
  --font-mono: 'JetBrains Mono', ui-monospace, Consolas, monospace;
  --ease: cubic-bezier(0.22, 1, 0.36, 1);
  --topbar-h: 56px;
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  min-height: 100dvh;
  background: var(--canvas);
  color: var(--ink);
  font-family: var(--font-sans);
  font-size: 0.9375rem;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
}
[hidden] { display: none !important; }
code { font-family: var(--font-mono); font-size: 0.85em; }
/* JetBrains Mono gộp '-->' thành mũi tên — tắt ligature để hiện đúng mã nguồn */
code, pre, textarea, .raw-lines, .txt-preview { font-variant-ligatures: none; font-feature-settings: 'liga' 0, 'calt' 0; }
.muted { color: var(--ink-3); font-size: 0.875rem; margin: 0; }

/* Nút */
.btn {
  font: inherit;
  font-weight: 500;
  font-size: 0.875rem;
  min-height: 40px;
  padding: 0 1rem;
  border-radius: 0.625rem;
  border: 1px solid transparent;
  cursor: pointer;
  transition: background-color 160ms var(--ease), border-color 160ms var(--ease), transform 120ms var(--ease);
  white-space: nowrap;
}
.btn:active:not(:disabled) { transform: translateY(1px); }
.btn:disabled { opacity: 0.45; cursor: not-allowed; }
.btn:focus-visible, .tab:focus-visible, select:focus-visible, textarea:focus-visible, .dropzone:focus-within {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}
.btn--primary { background: var(--primary-bg); color: #fff; }
.btn--primary:hover:not(:disabled) { background: var(--primary-hover); }
.btn--ghost { background: var(--paper); color: var(--ink); border-color: var(--line); }
.btn--ghost:hover:not(:disabled) { background: var(--wash); border-color: var(--line-strong); }
.btn--text { background: none; color: var(--accent); padding: 0; min-height: 32px; }
.btn--text:hover:not(:disabled) { text-decoration: underline; text-underline-offset: 3px; }
.btn--sm { min-height: 32px; padding: 0 0.7rem; font-size: 0.8125rem; }
.btn.is-busy { position: relative; color: transparent; pointer-events: none; overflow: hidden; }
.btn.is-busy::after {
  content: 'Đang tạo…';
  position: absolute; inset: 0;
  display: grid; place-items: center;
  color: #fff;
  background: linear-gradient(90deg, var(--primary-bg) 0%, #4A86C5 50%, var(--primary-bg) 100%) 0 0 / 200% 100%;
  animation: shimmer 1.2s linear infinite;
}

/* Thanh trên */
.topbar {
  position: sticky; top: 0; z-index: 20;
  height: var(--topbar-h);
  display: grid;
  grid-template-columns: 320px auto minmax(0, 1fr) auto;
  align-items: center;
  gap: 1rem;
  padding: 0 1.5rem 0 0;
  background: var(--paper);
  border-bottom: 1px solid var(--line);
}
.brand { display: flex; align-items: center; gap: 0.6rem; padding-left: 1.5rem; }
.brand__mark {
  width: 22px; height: 22px; border-radius: 6px;
  background:
    linear-gradient(var(--paper), var(--paper)) 6px 6px / 10px 2px no-repeat,
    linear-gradient(var(--paper), var(--paper)) 6px 10px / 10px 2px no-repeat,
    linear-gradient(var(--paper), var(--paper)) 6px 14px / 6px 2px no-repeat,
    var(--accent);
}
.brand__name { font-weight: 700; letter-spacing: -0.02em; font-size: 1rem; }
.topbar__file { display: flex; align-items: baseline; gap: 0.75rem; min-width: 0; }
.topbar__file-name { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.topbar__file-meta { font-family: var(--font-mono); font-size: 0.75rem; color: var(--ink-2); white-space: nowrap; }
.topbar__actions { display: flex; gap: 0.5rem; grid-column: 4; }

/* Menu ứng dụng trên cùng: convertSRS / Text → JSON / Mã hoá / Base64 */
.appnav { display: flex; align-items: stretch; align-self: stretch; gap: 0.25rem; margin-left: -1rem; }
.appnav__item {
  display: flex; align-items: center;
  padding: 0 0.95rem;
  font-size: 0.875rem; font-weight: 500; color: var(--ink-2); text-decoration: none; white-space: nowrap;
  border-bottom: 2px solid transparent; margin-bottom: -1px;
  transition: color 160ms var(--ease), border-color 160ms var(--ease);
}
.appnav__item:hover { color: var(--ink); }
.appnav__item.is-active { color: var(--ink); font-weight: 600; border-bottom-color: var(--accent); }
.appnav__item:focus-visible { outline: 2px solid var(--accent); outline-offset: -4px; border-radius: 0.4rem; }

/* Text → JSON / Mã hoá: toàn màn hình, ẩn phần chỉ dành cho tài liệu */
html:not([data-app='srs']) .sidebar,
html:not([data-app='srs']) .tabs,
html:not([data-app='srs']) .topbar__file,
html:not([data-app='srs']) #btnTxt,
html:not([data-app='srs']) .print-wrap,
html:not([data-app='srs']) #btnPdf { display: none !important; }
html:not([data-app='srs']) .layout { grid-template-columns: minmax(0, 1fr); }
html[data-app='json'] .pane:not([data-pane='json']),
html[data-app='crypto'] .pane:not([data-pane='crypto']) { display: none !important; }
html[data-app='json'] .pane[data-pane='json'],
html[data-app='crypto'] .pane[data-pane='crypto'] { display: block; }
html:not([data-app='srs']) .workspace { padding-top: 1.5rem; }

/* Bố cục */
.layout {
  display: grid;
  grid-template-columns: 320px minmax(0, 1fr);
  min-height: calc(100dvh - var(--topbar-h));
}
.sidebar {
  position: sticky; top: var(--topbar-h);
  height: calc(100dvh - var(--topbar-h));
  display: flex; flex-direction: column;
  background: var(--paper);
  border-right: 1px solid var(--line);
  overflow: hidden;
}
.panel { padding: 1.1rem 1.5rem 1.25rem; border-top: 1px solid var(--line); }
.panel:first-child { border-top: 0; }
.panel--grow { flex: 1; min-height: 0; display: flex; flex-direction: column; }
.panel--compact { padding-top: 0.6rem; padding-bottom: 0.7rem; }
.panel__head { display: flex; align-items: baseline; justify-content: space-between; gap: 0.5rem; }
.panel__head .panel__title { margin-bottom: 0.75rem; }
.btn--xs { min-height: 24px; font-size: 0.75rem; }
.source-quick { display: grid; grid-template-columns: 1fr 1fr; gap: 0.5rem; }
.panel--source:has(.source-body[hidden]) { padding-bottom: 0.9rem; }
.panel__title {
  margin: 0 0 0.75rem;
  font-size: 0.75rem; font-weight: 600;
  letter-spacing: 0.06em; text-transform: uppercase;
  color: var(--ink-2);
  display: flex; align-items: baseline; gap: 0.5rem;
}
.panel__count { font-family: var(--font-mono); font-weight: 400; letter-spacing: 0; color: var(--ink-3); }

.dropzone {
  display: grid; gap: 0.2rem;
  padding: 1rem;
  border: 1.5px dashed var(--line-strong);
  border-radius: 1rem;
  background: var(--paper);
  cursor: pointer;
  transition: border-color 160ms var(--ease), background-color 160ms var(--ease);
  margin-bottom: 0.5rem;
}
.dropzone input { position: absolute; width: 1px; height: 1px; opacity: 0; pointer-events: none; }
.dropzone:hover { border-color: var(--ink-3); }
.dropzone.is-over { border-color: var(--accent); background: var(--accent-tint); }
.dropzone__main { font-weight: 500; font-size: 0.875rem; }
.dropzone__sub { font-size: 0.8125rem; color: var(--ink-2); }

/* Chọn theme trên thanh trên */
.topbar__actions { align-items: center; }
.segmented.segmented--theme { margin: 0 0.5rem 0 0; grid-template-columns: repeat(3, auto); }
.segmented--theme .segmented__item { padding: 0 0.7rem; font-size: 0.78rem; }

/* Chọn cách nhập */
.segmented {
  display: grid; grid-template-columns: 1fr 1fr;
  padding: 3px; margin-bottom: 0.75rem;
  background: var(--canvas); border-radius: 0.625rem;
}
.segmented__item {
  font: inherit; font-size: 0.8125rem; font-weight: 500;
  min-height: 32px; border: 0; border-radius: 0.5rem;
  background: none; color: var(--ink-2); cursor: pointer;
  transition: background-color 160ms var(--ease), color 160ms var(--ease);
}
.segmented__item.is-active { background: var(--paper); color: var(--ink); box-shadow: 0 1px 2px rgba(24, 24, 27, 0.08); }
.paste-box {
  width: 100%; resize: vertical;
  padding: 0.6rem 0.75rem;
  border: 1px solid var(--line); border-radius: 0.625rem;
  font-family: var(--font-mono); font-size: 0.75rem; line-height: 1.55;
  color: var(--ink); background: var(--paper);
}
.paste-actions { display: flex; flex-wrap: wrap; gap: 0.4rem; margin: 0.5rem 0; }

/* Markdown gốc */
.paper--raw { padding: 0; overflow: hidden; }
.raw-head {
  display: flex; align-items: center; justify-content: space-between; gap: 1rem; flex-wrap: wrap;
  padding: 0.75rem 1.25rem;
  border-bottom: 1px solid var(--line);
  background: var(--wash);
}
.raw-head__label { font-size: 0.8125rem; color: var(--ink-2); }
.raw-head__tools { display: flex; align-items: center; gap: 0.5rem; }
.check--sm { font-size: 0.8125rem; min-height: 28px; margin-right: 0.25rem; }
.raw-view { overflow-x: auto; padding: 0.75rem 0; }
.raw-view .muted { padding: 1rem 1.25rem; }
.raw-lines {
  display: grid; grid-template-columns: max-content minmax(0, 1fr);
  font-family: var(--font-mono); font-size: 0.8rem; line-height: 1.65;
  counter-reset: ln;
}
.raw-lines .ln {
  padding: 0 0.9rem 0 1.25rem;
  text-align: right; color: var(--ink-3);
  user-select: none;
  border-right: 1px solid var(--line);
}
.raw-lines .lc { padding: 0 1.25rem 0 1rem; white-space: pre; min-width: 0; }
.raw-view.is-wrap .lc { white-space: pre-wrap; overflow-wrap: anywhere; }
.raw-lines .lc.hljs-section { color: var(--accent); font-weight: 600; }
.raw-lines .lc.is-fence { background: var(--accent-tint); }

/* Mục lục */
.toc { overflow-y: auto; margin: 0 -0.5rem; padding: 0 0.5rem; flex: 1; min-height: 0; }
.toc a {
  display: block;
  padding: 0.3rem 0.5rem;
  border-radius: 0.4rem;
  color: var(--ink-2);
  text-decoration: none;
  font-size: 0.8375rem;
  line-height: 1.4;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
  animation: rise 240ms var(--ease) both;
  animation-delay: calc(var(--i, 0) * 25ms);
}
.toc a:hover { background: var(--wash); color: var(--ink); }
.toc a.is-current { background: var(--accent-tint); color: var(--accent); font-weight: 500; }
.toc a.lvl-1 { font-weight: 600; color: var(--ink); }
.toc a.lvl-2 { padding-left: 1.1rem; }
.toc a.lvl-3 { padding-left: 1.9rem; font-size: 0.8rem; }
.toc__sub { margin: 1.1rem 0 0.4rem 0.5rem; }
.toc a.is-fig { padding-left: 0.5rem; font-size: 0.78rem; font-family: var(--font-mono); color: var(--accent); }

/* Form */
.field-row { display: grid; grid-template-columns: 1fr 1fr; gap: 0.75rem; margin-bottom: 0.75rem; }
.field { display: grid; gap: 0.35rem; }
.field__label { font-size: 0.8125rem; color: var(--ink-2); }
select {
  font: inherit; font-size: 0.875rem;
  height: 38px; padding: 0 0.6rem;
  border: 1px solid var(--line); border-radius: 0.5rem;
  background: var(--paper); color: var(--ink);
}
.check { display: flex; align-items: center; gap: 0.55rem; font-size: 0.875rem; min-height: 30px; cursor: pointer; }
.check input { accent-color: var(--accent); width: 16px; height: 16px; margin: 0; }
.panel .btn--text { margin-top: 0.4rem; }
.panel__hint { margin: 0.35rem 0 0; font-size: 0.75rem; line-height: 1.5; color: var(--ink-2); }
.panel__hint code { font-size: 0.72rem; }

/* Vùng làm việc */
.workspace { min-width: 0; padding: 0 1.25rem 3rem; }
.tabs {
  position: sticky; top: var(--topbar-h); z-index: 10;
  display: flex; align-items: center; gap: 1.5rem;
  background: var(--canvas);
  border-bottom: 1px solid var(--line);
  margin: 0 -1.25rem 1.5rem; padding: 0 1.25rem;
}
.tab {
  font: inherit; font-size: 0.875rem; font-weight: 500;
  background: none; border: 0; cursor: pointer;
  color: var(--ink-2);
  height: 46px;
  border-bottom: 2px solid transparent;
  margin-bottom: -1px;
}
.tab:hover { color: var(--ink); }
.tab.is-active { color: var(--ink); border-bottom-color: var(--accent); }
.tabs__status { margin-left: auto; font-family: var(--font-mono); font-size: 0.75rem; color: var(--ink-2); }
.tabs__status.is-error { color: var(--rose); }

.pane { display: none; }
.pane.is-active { display: block; animation: fade 200ms var(--ease); }

.paper {
  /* Tờ giấy trải hết bề rộng vùng làm việc, lề trong co giãn theo màn hình */
  max-width: none;
  margin: 0 auto;
  background: var(--paper);
  border: 1px solid var(--line);
  border-radius: 0.75rem;
  box-shadow: 0 24px 48px -24px var(--shadow);
  padding: 2.5rem clamp(1.25rem, 3vw, 3rem);
  min-height: 60dvh;
}
.paper--text { padding: 2rem 2.25rem; }

/* Trạng thái trống */
.empty { display: grid; grid-template-columns: minmax(0, 1.4fr) minmax(0, 1fr); gap: 3rem; align-items: start; padding: 1rem 0; }
.empty__title { font-size: clamp(1.5rem, 2.6vw, 2rem); line-height: 1.2; letter-spacing: -0.025em; margin: 0 0 1rem; max-width: 18ch; }
.empty__text { color: var(--ink-2); margin: 0 0 1.75rem; max-width: 52ch; line-height: 1.7; }
.empty__actions { display: flex; flex-wrap: wrap; gap: 0.75rem; }
.empty__aside { border-left: 1px solid var(--line); padding-left: 2rem; }
.empty__aside-title { font-size: 0.75rem; text-transform: uppercase; letter-spacing: 0.06em; color: var(--ink-2); margin: 0.3rem 0 0.9rem; font-weight: 600; }
.empty__list { list-style: none; padding: 0; margin: 0 0 1.25rem; display: grid; gap: 0.45rem; font-size: 0.875rem; color: var(--ink-2); }
.empty__list code { color: var(--accent); margin-right: 0.4rem; }
.empty__hint { font-size: 0.8125rem; color: var(--ink-2); line-height: 1.6; margin: 0; }

/* Skeleton */
.skeleton { display: grid; gap: 0.9rem; }
.sk {
  height: 14px; border-radius: 6px;
  background: linear-gradient(90deg, var(--sk-a) 0%, var(--sk-b) 50%, var(--sk-a) 100%) 0 0 / 200% 100%;
  animation: shimmer 1.3s linear infinite;
}
.sk--title { height: 30px; width: 55%; margin-bottom: 0.8rem; }
.sk--short { width: 62%; }
.sk--block { height: 220px; border-radius: 0.75rem; margin: 0.8rem 0; }

/* Công cụ cho sơ đồ trong bản xem trước */
.diagram__tools {
  position: absolute; top: 0.6rem; right: 0.6rem;
  display: flex; gap: 0.35rem;
  opacity: 0;
  transform: translateY(-2px);
  transition: opacity 160ms var(--ease), transform 160ms var(--ease);
}
.diagram__frame:hover .diagram__tools, .diagram__frame:focus-within .diagram__tools { opacity: 1; transform: none; }
.diagram__tools .btn { min-height: 28px; padding: 0 0.6rem; font-size: 0.75rem; }

/* Trình soạn thảo */
.editor { max-width: none; margin: 0 auto; }
.editor__hint { margin: 0 0 0.75rem; font-size: 0.8125rem; color: var(--ink-2); }
.editor textarea {
  width: 100%;
  min-height: calc(100dvh - 220px);
  resize: vertical;
  padding: 1.25rem 1.5rem 1.25rem 0.9rem;
  color: var(--ink);
  font-family: var(--font-mono); font-size: 0.8125rem; line-height: 1.65;
  tab-size: 2;
}

/* Ô nhập có cột số dòng (attachLineNumbers trong app.js): khung, nền, viền nằm ở wrapper */
.lnwrap {
  position: relative;
  display: grid; grid-template-columns: auto minmax(0, 1fr);
  border: 1px solid var(--line); border-radius: 0.75rem;
  background: var(--paper);
  overflow: hidden;
}
.lnwrap:focus-within { outline: 2px solid var(--accent); outline-offset: 2px; }
.lnwrap > textarea.lnwrap__input { border: 0; border-radius: 0; background: transparent; outline: none; white-space: pre; overflow: auto; }
.lnwrap__gutter {
  position: relative; overflow: hidden;
  min-width: calc(var(--ln-digits, 2) * 1ch + 1.4rem);
  border-right: 1px solid var(--line);
  background: var(--wash);
  font-family: var(--font-mono); font-size: 0.8125rem; color: var(--ink-3);
  text-align: right; user-select: none; pointer-events: none;
}
/* Cột số đặt tuyệt đối: không làm giãn chiều cao khung (ô nhập vẫn cuộn bên trong) */
.lnwrap__nums { position: absolute; top: 0; left: 0; right: 0; white-space: pre; padding: 0 0.6rem 0 0.8rem; will-change: transform; }
.editor .lnwrap__nums { padding-top: 1.25rem; line-height: 1.65; }
.lnwrap__err {
  position: absolute; left: 0; right: 0;
  background: color-mix(in srgb, var(--rose) 18%, transparent);
  border-left: 3px solid var(--rose);
}

/* Tab Text -> JSON */
.t2j { display: grid; gap: 1rem; }
.t2j__bar { display: flex; flex-wrap: wrap; align-items: end; gap: 0.75rem 1.25rem; }
.field--inline select { min-width: 180px; }
.t2j__from { margin-left: auto; }
.t2j__from select { max-width: 320px; }
.t2j__grid { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); gap: 1rem; }
/* Vùng nhập / kết quả: kéo thanh giữa để đổi độ rộng (app.js đặt grid-template-columns theo tỉ lệ đã lưu) */
.t2j__grid--split { grid-template-columns: minmax(0, 1fr) 0.75rem minmax(0, 1fr); gap: 0.25rem; }
.t2j__split {
  position: relative; cursor: col-resize; touch-action: none; border-radius: 0.4rem;
  margin-top: calc(40px + 0.5rem);
}
.t2j__split::before {
  content: ''; position: absolute; left: 50%; top: 0; bottom: 0; width: 2px; transform: translateX(-50%);
  border-radius: 2px; background: var(--line); transition: background 120ms var(--ease);
}
.t2j__split:hover::before, .t2j__split:focus-visible::before, .t2j__split.is-dragging::before { background: var(--accent); width: 3px; }
.t2j__split:focus-visible { outline: none; }
html.is-resizing, html.is-resizing * { cursor: col-resize !important; user-select: none !important; }
.t2j__col { display: grid; grid-template-rows: auto minmax(0, 1fr); min-width: 0; }
.t2j__head { display: flex; align-items: center; justify-content: space-between; gap: 0.75rem; min-height: 40px; margin-bottom: 0.5rem; }
.t2j__title { font-size: 0.75rem; font-weight: 600; letter-spacing: 0.06em; text-transform: uppercase; color: var(--ink-2); }
.t2j__status { margin-left: 0.5rem; font-family: var(--font-mono); font-size: 0.72rem; font-weight: 400; letter-spacing: 0; text-transform: none; }
.t2j__status.is-error { color: var(--rose); }
.t2j__tools { display: flex; gap: 0.4rem; }
#t2jInput, .t2j__output {
  width: 100%; min-height: calc(100dvh - 300px); margin: 0;
  padding: 1rem 1.15rem;
  border: 1px solid var(--line); border-radius: 0.75rem;
  background: var(--paper); color: var(--ink);
  font-family: var(--font-mono); font-size: 0.8125rem; line-height: 1.6;
  tab-size: 2;
}
#t2jInput.lnwrap__input { padding-left: 0.9rem; }
.t2j__col .lnwrap__nums { padding-top: 1rem; line-height: 1.6; }

/* Kết quả JSON dạng văn bản và cây: số dòng bên trái */
.t2j__output .code-ln { counter-reset: ln; }
.t2j__output .code-line { display: block; position: relative; padding-left: calc(var(--ln-digits, 2) * 1ch + 1.4rem); min-height: 1.6em; }
.t2j__output .code-line::before, .jt-line::before {
  position: absolute; left: 0; top: 0; bottom: 0;
  width: calc(var(--ln-digits, 2) * 1ch + 0.6rem); padding-right: 0.5rem;
  text-align: right; color: var(--ink-3); user-select: none;
  border-right: 1px solid var(--line);
}
.t2j__output .code-line::before { counter-increment: ln; content: counter(ln); }
.t2j__output:has(.code-ln), .t2j__output:has(.jt) { padding-left: 0.35rem; }
#t2jInput { resize: vertical; }
/* Khung kết quả cao cố định theo màn hình: nội dung dài cuộn bên trong (cả dọc lẫn ngang),
   thanh cuộn ngang luôn nằm trong khung nhìn, không phải kéo trang xuống cuối */
.t2j__out { display: grid; grid-template-rows: auto minmax(0, 1fr); min-height: 0; height: calc(100dvh - 300px); }
.t2j__out .t2j__output { min-height: 0; height: 100%; }
.t2j__out:has(.jt-bar:not([hidden])) .t2j__output { border-top-left-radius: 0; border-top-right-radius: 0; }

/* Cây JSON thu gọn được */
.jt-bar {
  display: flex; flex-wrap: wrap; align-items: center; gap: 0.25rem 0.9rem;
  padding: 0.35rem 0.9rem;
  border: 1px solid var(--line); border-bottom: 0; border-radius: 0.75rem 0.75rem 0 0;
  background: var(--wash);
  font-size: 0.78rem; color: var(--ink-2);
}
.jt-level { display: inline-flex; align-items: center; gap: 0.35rem; }
.jt-level select { height: 26px; padding: 0 0.3rem; font-size: 0.75rem; }
.jt-focus { display: inline-flex; align-items: center; gap: 0.4rem; margin-left: auto; min-width: 0; }
.jt-focus__path { color: var(--accent); background: var(--accent-tint); padding: 0.1rem 0.4rem; border-radius: 0.3rem; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; max-width: 40ch; }
.jt { font-family: var(--font-mono); font-size: 0.8125rem; line-height: 1.6; --jt-gutter: calc(var(--ln-digits, 2) * 1ch + 1.1rem); padding-left: calc(var(--jt-gutter) + 1.3em); }
.jt-line { position: relative; display: flex; align-items: baseline; min-height: 1.6em; white-space: pre; border-radius: 0.3rem; }
/* Số dòng của cây JSON: nằm trong cột trái, theo bản in đẹp của JSON (thu gọn thì nhảy cóc) */
.jt-line::before { content: attr(data-ln); left: calc(-1 * var(--jt-gutter) - 1.3em); padding-right: 0.5rem; width: calc(var(--jt-gutter) - 1.1rem); }
.jt-line:hover { background: var(--wash); }
.jt-tog {
  flex: none; width: 1.2em; margin-left: -1.3em; padding: 0; border: 0; background: none;
  color: var(--ink-2); cursor: pointer; font: inherit; font-size: 1.05em; line-height: inherit;
  transition: transform 120ms var(--ease), color 120ms var(--ease);
}
.jt-tog:hover { color: var(--accent); }
.jt-node.is-collapsed > .jt-line > .jt-tog { transform: rotate(-90deg); }
.jt-node.is-collapsed > .jt-children, .jt-node.is-collapsed > .jt-close { display: none; }
.jt-summary { display: none; color: var(--ink-3); cursor: pointer; }
.jt-node.is-collapsed > .jt-line .jt-summary { display: inline; }
.jt-node.is-collapsed > .jt-line .jt-open-comma { display: inline; }
.jt-open-comma { display: none; }
.jt-count { color: var(--ink-3); font-style: italic; margin-left: 0.6em; }
.jt-node:not(.is-collapsed) > .jt-line .jt-count { display: none; }
.jt-actions { display: none; gap: 0.3rem; margin-left: 1rem; }
.jt-line:hover .jt-actions, .jt-line:focus-within .jt-actions { display: inline-flex; }
.jt-actions button {
  font: inherit; font-family: var(--font-sans); font-size: 0.7rem;
  padding: 0 0.45rem; height: 1.45em; line-height: 1;
  border: 1px solid var(--line); border-radius: 0.35rem; background: var(--paper); color: var(--ink-2); cursor: pointer;
}
.jt-actions button:hover { color: var(--accent); border-color: var(--accent); }
.jt-node.is-focus > .jt-line { background: var(--accent-tint); }
.jt-brace { cursor: pointer; }
.t2j__output { overflow: auto; white-space: pre; }
.t2j__output.is-error { color: var(--rose); white-space: pre-wrap; }
.t2j__output .hljs-attr { color: var(--accent); }
@media (max-width: 960px) {
  .t2j__grid, .t2j__grid--split { grid-template-columns: minmax(0, 1fr) !important; }
  .t2j__split { display: none; }
  #t2jInput, .t2j__output { min-height: 260px; }
  .t2j__out { height: min(70dvh, 560px); }
  .t2j__from { margin-left: 0; }
}

/* Tab Mã hoá / Base64 */
.segmented.segmented--mode { grid-template-columns: repeat(6, auto); margin: 0; }
.kg { display: grid; gap: 0.75rem; }
.kg[hidden], .t2j__grid[hidden] { display: none; }
.kg-info {
  display: flex; flex-wrap: wrap; gap: 0.35rem 1.5rem;
  padding: 0.6rem 0.9rem; border: 1px solid var(--line); border-radius: 0.6rem; background: var(--wash);
  font-size: 0.8rem; color: var(--ink-2);
}
.kg-info b { color: var(--ink); font-weight: 600; }
.kg-info code { font-size: 0.75rem; word-break: break-all; }
.kg-info .is-ok { color: #15803D; font-weight: 600; }
html[data-theme='dark'] .kg-info .is-ok { color: #4ADE80; }
.kg-info .is-error { color: var(--rose); font-weight: 600; }
.kg-out { white-space: pre-wrap; word-break: break-all; min-height: calc(100dvh - 380px); font-size: 0.78rem; }
/* PEM: giữ nguyên dòng 64 ký tự (không ngắt giữa dòng gây hiểu nhầm khoá bị hỏng), cuộn ngang nếu hẹp */
.kg-out.is-pem { white-space: pre; word-break: normal; overflow-x: auto; }
.kg-out.is-busy { opacity: 0.5; }
.segmented--mode .segmented__item { padding: 0 0.9rem; min-height: 36px; }
.ct-modes { display: flex; }
/* Hàng tuỳ chọn cao cố định bằng một ô có nhãn: chuyển chế độ không làm nội dung bên dưới nhảy */
.ct-optrow { display: flex; align-items: flex-end; min-height: 64px; }
.ct-opts { display: inline-flex; flex-wrap: wrap; align-items: end; gap: 0.75rem 1.25rem; }
.ct-opts .check { min-height: 38px; }
/* Sinh cặp khoá: 4 ô chọn chia đều một hàng + nút, không gãy dòng trên màn laptop */
@media (min-width: 641px) {
  .ct-optrow .ct-opts[data-ct-for='keygen'] { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)) auto auto; align-items: end; width: 100%; }
  .ct-optrow .ct-opts[data-ct-for='keygen'][hidden] { display: none; }
  .ct-opts[data-ct-for='keygen'] select { min-width: 0; width: 100%; }
  /* Mục đích chỉ có với RSA: giữ chỗ để các ô khác không đổi vị trí */
  .ct-opts[data-ct-for='keygen'] #kgPurposeField[hidden] { display: grid !important; visibility: hidden; }
}
.ct-hint { min-height: 1.5em; }
.ct-opts[hidden] { display: none; }
.ct-key input {
  height: 38px; min-width: 240px; padding: 0 0.6rem;
  border: 1px solid var(--line); border-radius: 0.5rem;
  background: var(--paper); color: var(--ink); font-family: var(--font-mono); font-size: 0.8125rem;
}
/* Sinh password */
.pw-len input {
  height: 38px; width: 6.5rem; padding: 0 0.6rem;
  border: 1px solid var(--line); border-radius: 0.5rem;
  background: var(--paper); color: var(--ink); font-family: var(--font-mono); font-size: 0.875rem;
}
.ct-key.pw-symset input { min-width: 0; width: 16rem; }
.ct-key.pw-symset input:disabled { opacity: 0.5; }
.pw { display: grid; gap: 0.6rem; max-width: 960px; }
.pw[hidden] { display: none; }
.pw-box {
  display: flex; flex-wrap: wrap; align-items: center; gap: 0.75rem 1rem;
  padding: 1rem 1.15rem; border: 1px solid var(--line); border-radius: 0.75rem; background: var(--paper);
}
.pw-out {
  flex: 1 1 20rem; min-width: 0;
  font-family: var(--font-mono); font-size: 1.35rem; line-height: 1.5; letter-spacing: 0.04em;
  word-break: break-all; color: var(--ink); user-select: all;
}
.pw-out.is-error { font-family: var(--font-sans); font-size: 0.875rem; letter-spacing: 0; color: var(--rose); user-select: text; }
.pw-out .pw-d { color: var(--accent); }
.pw-out .pw-s { color: var(--rose); }
.pw-actions { display: flex; gap: 0.5rem; flex: none; }
.pw-meter { display: grid; grid-template-columns: repeat(4, 1fr); gap: 0.3rem; }
.pw-meter span { height: 5px; border-radius: 3px; background: var(--line); transition: background 160ms var(--ease); }
.pw-meter[data-level='1'] span:nth-child(-n+1) { background: var(--rose); }
.pw-meter[data-level='2'] span:nth-child(-n+2) { background: #D97706; }
.pw-meter[data-level='3'] span:nth-child(-n+3) { background: #16A34A; }
.pw-meter[data-level='4'] span { background: #15803D; }
.pw-info { font-size: 0.8rem; color: var(--ink-2); }
.ct-note { font-size: 0.78rem; color: var(--ink-2); align-self: center; }
.ct-hint { margin: -0.4rem 0 0; font-size: 0.78rem; line-height: 1.5; color: var(--ink-2); }
.ct-hint[hidden] { display: none; }
.ct-file { position: relative; display: inline-grid; place-items: center; cursor: pointer; }
.ct-file input { position: absolute; inset: 0; opacity: 0; cursor: pointer; width: 100%; }
.ct-filebar {
  display: flex; align-items: center; justify-content: space-between; gap: 0.75rem;
  margin-bottom: 0.5rem; padding: 0.4rem 0.75rem;
  border: 1px solid var(--accent); border-radius: 0.5rem; background: var(--accent-tint);
  font-size: 0.8125rem; color: var(--ink);
}
.ct-filebar[hidden] { display: none; }
#ctInput { width: 100%; min-height: calc(100dvh - 330px); resize: vertical; margin: 0; padding: 1rem 1.15rem; border: 1px solid var(--line); border-radius: 0.75rem; background: var(--paper); color: var(--ink); font-family: var(--font-mono); font-size: 0.8125rem; line-height: 1.6; }
#ctInput:disabled { opacity: 0.55; }
.ct-output { white-space: pre-wrap; word-break: break-all; min-height: calc(100dvh - 330px); }
.ct-output.is-binary { white-space: pre; word-break: normal; }
@media (max-width: 960px) {
  #ctInput, .ct-output { min-height: 220px; }
  .segmented.segmented--mode { grid-template-columns: repeat(2, 1fr); width: 100%; }
  .kg-out { min-height: 200px; }
  .ct-key input { min-width: 0; width: 100%; }
  .pw-actions { width: 100%; }
  .pw-actions .btn { flex: 1; }
}

.txt-preview {
  margin: 0;
  font-family: var(--font-mono); font-size: 0.78rem; line-height: 1.6;
  white-space: pre; overflow-x: auto;
  color: var(--ink);
}

/* Hộp xem sơ đồ */
.viewer {
  width: min(1400px, 96vw); height: 92dvh;
  max-width: none; max-height: none;
  padding: 0; border: 1px solid var(--line); border-radius: 0.875rem;
  background: var(--paper); color: var(--ink);
  box-shadow: 0 30px 80px -20px rgba(24, 24, 27, 0.35);
}
.viewer[open] { display: grid; grid-template-rows: auto minmax(0, 1fr); }
.viewer::backdrop { background: var(--backdrop); }
.viewer__bar { display: flex; align-items: center; gap: 1rem; padding: 0.75rem 1rem; border-bottom: 1px solid var(--line); }
.viewer__title { font-weight: 600; font-size: 0.9rem; flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.viewer__tools { display: flex; align-items: center; gap: 0.4rem; }
.viewer__zoom { font-family: var(--font-mono); font-size: 0.75rem; min-width: 3.5rem; text-align: center; color: var(--ink-2); }
.viewer__stage { overflow: auto; background: var(--wash); padding: 2rem; cursor: grab; }
.viewer__stage.is-panning { cursor: grabbing; user-select: none; }
.viewer__stage svg { display: block; margin: 0 auto; max-width: none !important; height: auto; }

.toast {
  position: fixed; bottom: 1.5rem; left: 50%; z-index: 50;
  transform: translateX(-50%);
  max-width: min(560px, 92vw);
  padding: 0.75rem 1.1rem;
  border-radius: 0.625rem;
  background: var(--ink); color: #fff;
  font-size: 0.875rem;
  box-shadow: 0 16px 40px -16px rgba(24, 24, 27, 0.5);
  animation: rise 220ms var(--ease);
}
.toast.is-error { background: var(--rose); }

/* Nút máy in + menu tuỳ chọn in */
.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; }
.btn--icon { width: 40px; padding: 0; display: inline-grid; place-items: center; color: var(--ink-2); }
.btn--icon:hover:not(:disabled), .btn--icon.is-active { color: var(--ink); }
.btn--icon.is-active { background: var(--wash); border-color: var(--line-strong); }
.print-wrap { position: relative; }
.print-menu {
  position: absolute; top: calc(100% + 8px); right: 0; z-index: 40;
  width: 300px;
  padding: 1rem 1.1rem 1.1rem;
  background: var(--paper);
  border: 1px solid var(--line);
  border-radius: 0.875rem;
  box-shadow: 0 24px 48px -20px var(--shadow);
  animation: rise 160ms var(--ease);
}
.print-menu__head { font-size: 0.75rem; font-weight: 600; letter-spacing: 0.06em; text-transform: uppercase; color: var(--ink-2); margin-bottom: 0.85rem; }
.print-menu__hint { margin: 0.5rem 0 0; font-size: 0.75rem; line-height: 1.5; color: var(--ink-2); }
.print-menu__actions { display: grid; grid-template-columns: 1fr 1fr; gap: 0.5rem; margin-top: 0.9rem; padding-top: 0.9rem; border-top: 1px solid var(--line); }

/* Lớp phủ khi kéo file vào trang — không chặn chuột để sự kiện kéo thả đi tới phần tử bên dưới */
.drop-overlay {
  position: fixed; inset: 0; z-index: 60;
  display: grid; place-items: center;
  padding: 1.25rem;
  background: color-mix(in srgb, var(--canvas) 72%, transparent);
  pointer-events: none;
  animation: fade 140ms var(--ease);
}
.drop-overlay::before {
  content: '';
  position: absolute; inset: 0.75rem;
  border: 2px dashed var(--accent);
  border-radius: 1rem;
  background: color-mix(in srgb, var(--accent-tint) 55%, transparent);
}
.drop-overlay__box {
  position: relative;
  display: grid; gap: 0.35rem; justify-items: center;
  padding: 1.5rem 2rem;
  background: var(--paper);
  border: 1px solid var(--line);
  border-radius: 0.875rem;
  box-shadow: 0 24px 48px -24px var(--shadow);
  animation: rise 180ms var(--ease);
}
.drop-overlay__title { font-weight: 700; font-size: 1.125rem; letter-spacing: -0.01em; }
.drop-overlay__sub { font-size: 0.8125rem; color: var(--ink-2); }
body.is-dragging .viewer { pointer-events: none; }

/* ---------- Giao diện tối ---------- */
html[data-theme='dark'] {
  color-scheme: dark;
  --canvas: #111113;
  --paper: #18181B;
  --ink: #E4E4E7;
  --ink-2: #A1A1AA;
  --ink-3: #71717A;
  --line: #2E2E33;
  --line-strong: #3F3F46;
  --wash: #1F1F23;
  --accent: #6AA5E0;
  --accent-hover: #84B6E8;
  --accent-tint: #1B2A3A;
  --rose: #FB7185;
  --primary-bg: #2F74BF;
  --primary-hover: #3A82D0;
  --sk-a: #232327;
  --sk-b: #2C2C31;
  --shadow: rgba(0, 0, 0, 0.55);
  --backdrop: rgba(9, 9, 11, 0.7);
}
html[data-theme='dark'] .doc {
  --ink: #E4E4E7;
  --ink-2: #A1A1AA;
  --ink-3: #71717A;
  --line: #2E2E33;
  --wash: #1F1F23;
  --accent: #6AA5E0;
  --accent-tint: #1B2A3A;
  --rose: #FB7185;
}
html[data-theme='dark'] .toast { background: #E4E4E7; color: #18181B; }
html[data-theme='dark'] .toast.is-error { background: #BE123C; color: #fff; }
html[data-theme='dark'] .doc .diagram--error .diagram__frame { background: #2A1418; border-color: #5B2530; border-left-color: var(--rose); }
html[data-theme='dark'] .doc .diagram__error-src { background: var(--paper); }
html[data-theme='dark'] .segmented__item.is-active { box-shadow: 0 1px 2px rgba(0, 0, 0, 0.5); }
/* highlight.js: đè bảng màu github sáng bằng github-dark */
html[data-theme='dark'] .hljs { color: #C9D1D9; }
html[data-theme='dark'] .hljs-doctag, html[data-theme='dark'] .hljs-keyword, html[data-theme='dark'] .hljs-meta .hljs-keyword,
html[data-theme='dark'] .hljs-template-tag, html[data-theme='dark'] .hljs-template-variable, html[data-theme='dark'] .hljs-type,
html[data-theme='dark'] .hljs-variable.language_ { color: #FF7B72; }
html[data-theme='dark'] .hljs-title, html[data-theme='dark'] .hljs-title.class_, html[data-theme='dark'] .hljs-title.function_ { color: #D2A8FF; }
html[data-theme='dark'] .hljs-attr, html[data-theme='dark'] .hljs-attribute, html[data-theme='dark'] .hljs-literal,
html[data-theme='dark'] .hljs-meta, html[data-theme='dark'] .hljs-number, html[data-theme='dark'] .hljs-operator,
html[data-theme='dark'] .hljs-variable, html[data-theme='dark'] .hljs-selector-attr, html[data-theme='dark'] .hljs-selector-class,
html[data-theme='dark'] .hljs-selector-id { color: #79C0FF; }
html[data-theme='dark'] .hljs-regexp, html[data-theme='dark'] .hljs-string, html[data-theme='dark'] .hljs-meta .hljs-string { color: #A5D6FF; }
html[data-theme='dark'] .hljs-built_in, html[data-theme='dark'] .hljs-symbol { color: #FFA657; }
html[data-theme='dark'] .hljs-comment, html[data-theme='dark'] .hljs-code, html[data-theme='dark'] .hljs-formula { color: #8B949E; }
html[data-theme='dark'] .hljs-name, html[data-theme='dark'] .hljs-quote, html[data-theme='dark'] .hljs-selector-tag,
html[data-theme='dark'] .hljs-selector-pseudo { color: #7EE787; }
html[data-theme='dark'] .hljs-subst { color: #C9D1D9; }
html[data-theme='dark'] .hljs-section { color: #1F6FEB; }
html[data-theme='dark'] .hljs-bullet { color: #F2CC60; }
html[data-theme='dark'] .hljs-addition { color: #AFF5B4; background-color: #033A16; }
html[data-theme='dark'] .hljs-deletion { color: #FFDCD7; background-color: #67060C; }

@keyframes shimmer { to { background-position: -200% 0; } }
@keyframes rise { from { opacity: 0; transform: translateY(4px); } to { opacity: 1; transform: none; } }
@keyframes fade { from { opacity: 0; } to { opacity: 1; } }

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

/* Responsive */
@media (max-width: 960px) {
  .topbar { grid-template-columns: auto auto minmax(0, 1fr) auto; padding-right: 1rem; }
  .appnav { margin-left: 0; }
  .appnav__item { padding: 0 0.6rem; }
  .brand { padding-left: 1rem; }
  .layout { grid-template-columns: minmax(0, 1fr); }
  /* Mobile: tài liệu lên trước, sidebar (nguồn, mục lục, tuỳ chọn) xuống dưới */
  .sidebar { position: static; height: auto; border-right: 0; border-top: 1px solid var(--line); order: 2; }
  .workspace { order: 1; }
  .panel--grow { max-height: 55dvh; }
  .workspace { padding: 0 1rem 3rem; }
  .tabs { margin: 0 -1rem 1.25rem; padding: 0 1rem; gap: 1rem; }
  .paper { padding: 1.75rem 1.25rem; }
  .diagram__tools { opacity: 1; transform: none; }
}
@media (max-width: 640px) {
  .topbar { height: auto; grid-template-columns: minmax(0, 1fr); padding: 0.75rem 1rem; gap: 0.6rem; }
  .brand { padding-left: 0; }
  .topbar__file { display: none; }
  .appnav { grid-column: 1; margin: 0 -1rem; padding: 0 0.5rem; overflow-x: auto; border-top: 1px solid var(--line); min-height: 42px; scrollbar-width: none; }
  .appnav__item { flex: 1 0 auto; justify-content: center; }
  .topbar__actions { grid-column: 1; display: grid; grid-template-columns: 1fr auto 1fr; }
  html:not([data-app='srs']) .topbar__actions { display: block; }
  .btn--icon { width: 44px; min-height: 44px; }
  .print-menu { position: fixed; top: auto; left: 1rem; right: 1rem; width: auto; margin-top: 8px; }
  .segmented.segmented--theme { grid-column: 1 / -1; margin: 0; grid-template-columns: repeat(3, 1fr); }
  .segmented--theme .segmented__item { min-height: 36px; }
  .topbar__actions .btn { min-height: 44px; }
  .tabs { position: static; overflow-x: auto; scrollbar-width: none; }
  .tab { white-space: nowrap; flex: none; }
  .tabs__status { display: none; }
  .workspace { padding: 0 0.5rem 2rem; }
  .tabs { margin: 0 -0.5rem 1rem; padding: 0 0.5rem; }
  .paper { padding: 1.25rem 1rem; border-radius: 0.5rem; }
  .empty { grid-template-columns: minmax(0, 1fr); gap: 2rem; }
  .empty__aside { border-left: 0; padding-left: 0; border-top: 1px solid var(--line); padding-top: 1.5rem; }
  .empty__actions .btn { flex: 1 1 100%; min-height: 44px; }
}

@media print {
  .topbar, .sidebar, .tabs, .toast, .viewer, .pane:not([data-pane='preview']) { display: none !important; }
  body { background: #fff; }
  .layout { display: block; }
  .workspace { padding: 0; }
  .pane[data-pane='preview'] { display: block !important; }
  .paper { border: 0; box-shadow: none; padding: 0; max-width: none; }
}
