/* ===== VietOCR System UI ===== */
:root {
  --bg: #f4f6fb;
  --panel: #ffffff;
  --ink: #16192b;
  --muted: #6b7280;
  --line: #e5e8f0;
  --accent: #4f46e5;
  --accent-2: #6d5cf6;
  --accent-soft: #eef0ff;
  --ok: #059669;
  --ok-soft: #ecfdf5;
  --warn: #d97706;
  --warn-soft: #fffbeb;
  --err: #dc2626;
  --err-soft: #fef2f2;
  --side: #12142b;
  --side-ink: #b9bdd8;
  --radius: 14px;
  --shadow: 0 1px 2px rgba(22, 25, 43, .05), 0 8px 24px -12px rgba(22, 25, 43, .12);
}
* { box-sizing: border-box; margin: 0; padding: 0; }
html { -webkit-text-size-adjust: 100%; }
body {
  font: 15px/1.55 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  background: var(--bg); color: var(--ink);
}
a { color: var(--accent); text-decoration: none; }

/* ===== shell ===== */
.shell { display: flex; min-height: 100vh; }
.sidebar {
  width: 248px; flex-shrink: 0; background: var(--side); color: var(--side-ink);
  display: flex; flex-direction: column; position: sticky; top: 0; height: 100vh;
}
.brand { display: flex; align-items: center; gap: 12px; padding: 22px 20px 18px; }
.brand-logo {
  width: 40px; height: 40px; border-radius: 11px; flex-shrink: 0;
  background: linear-gradient(135deg, var(--accent), #9333ea);
  color: #fff; font-weight: 800; font-size: 13px; letter-spacing: .5px;
  display: flex; align-items: center; justify-content: center;
  box-shadow: 0 4px 14px -4px rgba(99, 102, 241, .7);
}
.brand-logo.big { width: 54px; height: 54px; font-size: 16px; border-radius: 15px; margin: 0 auto; }
.brand-name { color: #fff; font-weight: 700; font-size: 16px; line-height: 1.2; }
.brand-name span { display: block; font-weight: 400; font-size: 11.5px; color: var(--side-ink); letter-spacing: 1.5px; text-transform: uppercase; }
.sidebar nav { flex: 1; padding: 8px 12px; overflow-y: auto; }
.sidebar nav a {
  display: flex; align-items: center; gap: 11px; color: var(--side-ink);
  padding: 10px 12px; border-radius: 9px; margin-bottom: 2px; font-size: 14.5px;
  transition: background .15s, color .15s;
}
.sidebar nav a:hover { background: rgba(255, 255, 255, .06); color: #fff; }
.sidebar nav a.on { background: linear-gradient(135deg, rgba(99, 102, 241, .28), rgba(147, 51, 234, .18)); color: #fff; font-weight: 600; }
.sidebar nav .ic { width: 20px; text-align: center; font-size: 15px; opacity: .9; }
.nav-sep { font-size: 10.5px; letter-spacing: 1.5px; text-transform: uppercase; color: #5a5f85; padding: 16px 12px 6px; }
.sidebar-foot { border-top: 1px solid rgba(255, 255, 255, .08); padding: 14px 16px; display: flex; align-items: center; gap: 10px; }
.me { display: flex; align-items: center; gap: 10px; flex: 1; min-width: 0; }
.avatar {
  width: 34px; height: 34px; border-radius: 50%; flex-shrink: 0;
  background: linear-gradient(135deg, #38bdf8, #4f46e5); color: #fff;
  display: flex; align-items: center; justify-content: center; font-weight: 700; font-size: 14px;
}
.me-name { color: #fff; font-size: 13.5px; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.me-role { font-size: 11.5px; color: var(--side-ink); }
.logout { color: var(--side-ink); font-size: 18px; padding: 6px; border-radius: 8px; }
.logout:hover { background: rgba(255, 255, 255, .08); color: #fff; }

.main { flex: 1; min-width: 0; display: flex; flex-direction: column; }
.topbar {
  padding: 22px 32px 0;
}
.topbar h1 { font-size: 22px; font-weight: 700; letter-spacing: -.02em; }
.content { padding: 20px 32px 44px; }

/* ===== cards & grids ===== */
.grid { display: grid; gap: 16px; }
.grid.cols-4 { grid-template-columns: repeat(4, 1fr); }
.grid.cols-3 { grid-template-columns: repeat(3, 1fr); }
.grid.cols-2 { grid-template-columns: repeat(2, 1fr); }
@media (max-width: 1100px) { .grid.cols-4 { grid-template-columns: repeat(2, 1fr); } .grid.cols-3 { grid-template-columns: 1fr; } }
@media (max-width: 900px) { .grid.cols-2 { grid-template-columns: 1fr; } }
.card {
  background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 20px 22px; box-shadow: var(--shadow);
}
.card h2 { font-size: 15.5px; font-weight: 700; margin-bottom: 14px; }
.stat .num { font-size: 30px; font-weight: 800; letter-spacing: -.03em; }
.stat .lbl { color: var(--muted); font-size: 13px; margin-top: 2px; }
.stat .ic {
  float: right; width: 42px; height: 42px; border-radius: 11px; font-size: 19px;
  display: flex; align-items: center; justify-content: center;
  background: var(--accent-soft); color: var(--accent);
}

/* ===== badges, alerts ===== */
.badge { display: inline-block; padding: 3px 10px; border-radius: 999px; font-size: 12px; font-weight: 600; }
.badge.ok { background: var(--ok-soft); color: var(--ok); }
.badge.err { background: var(--err-soft); color: var(--err); }
.badge.warn { background: var(--warn-soft); color: var(--warn); }
.badge.gray { background: #f1f2f6; color: var(--muted); }
.alert { padding: 12px 16px; border-radius: 10px; margin-bottom: 16px; font-size: 14px; }
.alert.error { background: var(--err-soft); color: #991b1b; border: 1px solid #fecaca; }
.alert.ok { background: var(--ok-soft); color: #065f46; border: 1px solid #a7f3d0; }

/* ===== buttons ===== */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  padding: 9px 18px; border-radius: 10px; border: 1px solid var(--line);
  background: #fff; color: var(--ink); font-size: 14px; font-weight: 600;
  cursor: pointer; transition: all .15s; text-decoration: none;
}
.btn:hover { border-color: #c9cede; background: #fafbff; }
.btn.primary {
  background: linear-gradient(135deg, var(--accent), var(--accent-2));
  color: #fff; border: none; box-shadow: 0 4px 14px -6px rgba(79, 70, 229, .6);
}
.btn.primary:hover { filter: brightness(1.07); }
.btn.danger { color: var(--err); border-color: #fecaca; background: #fff; }
.btn.danger:hover { background: var(--err-soft); }
.btn.sm { padding: 6px 12px; font-size: 13px; border-radius: 8px; }
.btn.wide { width: 100%; }
.btn:disabled { opacity: .5; cursor: not-allowed; }

/* ===== forms ===== */
label { display: block; font-size: 13.5px; font-weight: 600; color: #374151; margin-bottom: 14px; }
input[type=text], input[type=password], input[type=number], select, textarea {
  width: 100%; margin-top: 6px; padding: 10px 13px; font-size: 14.5px;
  border: 1.5px solid var(--line); border-radius: 10px; background: #fff; color: var(--ink);
  font-family: inherit; transition: border .15s, box-shadow .15s;
}
input:focus, select:focus, textarea:focus {
  outline: none; border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(79, 70, 229, .12);
}

/* ===== tables ===== */
.tbl-wrap { overflow-x: auto; }
table.tbl { width: 100%; border-collapse: collapse; font-size: 14px; }
.tbl th {
  text-align: left; font-size: 12px; text-transform: uppercase; letter-spacing: .06em;
  color: var(--muted); font-weight: 600; padding: 10px 12px; border-bottom: 1px solid var(--line);
  white-space: nowrap;
}
.tbl td { padding: 12px; border-bottom: 1px solid var(--line); vertical-align: middle; }
.tbl tr:last-child td { border-bottom: none; }
.tbl tr:hover td { background: #fafbfe; }
.mono { font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; font-size: 13px; }

/* ===== login ===== */
.login-bg {
  min-height: 100vh; display: flex; align-items: center; justify-content: center;
  background: radial-gradient(1000px 600px at 20% -10%, #312e81 0%, transparent 55%),
              radial-gradient(900px 600px at 110% 110%, #4c1d95 0%, transparent 50%),
              #12142b;
  padding: 20px;
}
.login-wrap { width: 100%; max-width: 400px; }
.login-card {
  background: #fff; border-radius: 20px; padding: 36px 34px 30px;
  box-shadow: 0 30px 70px -20px rgba(0, 0, 0, .55);
}
.login-head { text-align: center; margin-bottom: 22px; }
.login-head h1 { font-size: 21px; margin-top: 14px; letter-spacing: -.02em; }
.login-head p { color: var(--muted); font-size: 13.5px; margin-top: 4px; }
.login-foot { text-align: center; color: rgba(255, 255, 255, .45); font-size: 12.5px; margin-top: 18px; }

/* ===== scan page: upload on top, result below ===== */
.scan-page { display: flex; flex-direction: column; gap: 16px; }
.scan-grid { display: grid; grid-template-columns: 1fr 1.1fr; gap: 22px; align-items: start; }
.scan-controls { display: flex; flex-direction: column; }
.scan-actions { margin-top: 18px; display: flex; gap: 10px; align-items: center; flex-wrap: wrap; }
@media (max-width: 860px) { .scan-grid { grid-template-columns: 1fr; gap: 16px; } }

/* ===== dropzone — fixed size so adding a file never shifts the layout ===== */
.dropzone {
  border: 2px dashed #c7cbe0; border-radius: var(--radius); padding: 20px;
  text-align: center; cursor: pointer; transition: border-color .2s, background .2s; background: #fbfcff;
  min-height: 200px; display: flex; flex-direction: column; align-items: center; justify-content: center;
}
.dropzone:hover, .dropzone.drag { border-color: var(--accent); background: var(--accent-soft); }
.dropzone .dz-ic { font-size: 40px; }
.dropzone p { color: var(--muted); font-size: 13.5px; margin-top: 6px; }
.dropzone strong { color: var(--ink); font-size: 15.5px; display: block; margin-top: 10px; }
/* reserved slot below the dropzone; keeps height stable whether or not a file is chosen */
.filechip-slot { min-height: 44px; display: flex; align-items: center; margin-top: 10px; }
.file-chip {
  display: inline-flex; align-items: center; gap: 10px; max-width: 100%;
  background: var(--accent-soft); color: var(--accent); font-weight: 600; font-size: 13.5px;
  padding: 8px 14px; border-radius: 999px;
}
.file-chip span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.file-chip button { border: none; background: none; color: inherit; cursor: pointer; font-size: 15px; flex-shrink: 0; }

.spinner {
  width: 22px; height: 22px; border: 3px solid rgba(79, 70, 229, .2); border-top-color: var(--accent);
  border-radius: 50%; animation: spin .7s linear infinite; display: inline-block; vertical-align: middle;
}
@keyframes spin { to { transform: rotate(360deg); } }

pre.json {
  background: #0f172a; color: #e2e8f0; border-radius: 12px; padding: 18px;
  font-size: 13px; line-height: 1.6; max-height: 520px; overflow-y: auto;
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  /* wrap long lines (e.g. raw_text) instead of overflowing the page horizontally */
  white-space: pre-wrap; overflow-wrap: anywhere; word-break: break-word;
}
.kv-table td:first-child { font-weight: 600; color: #374151; white-space: nowrap; width: 200px; }
.copy-btn { float: right; }

/* ===== handwriting OCR suggestions ===== */
.suggestions { margin: 16px 0; display: grid; gap: 10px; }
.suggestions-head { display: flex; align-items: baseline; gap: 10px; flex-wrap: wrap; }
.suggestions-head span { color: var(--muted); font-size: 12.5px; }
.suggestion-card {
  display: grid; grid-template-columns: 42px minmax(0, 1fr) 74px; gap: 12px;
  align-items: center; padding: 13px 14px; border: 1px solid var(--line);
  border-radius: 12px; background: #fbfcff;
}
.suggestion-card:first-of-type { border-color: #a5b4fc; background: var(--accent-soft); }
.suggestion-rank { font-weight: 800; color: var(--accent); }
.suggestion-text { white-space: pre-wrap; overflow-wrap: anywhere; margin-bottom: 7px; }
.confidence-track { height: 6px; border-radius: 999px; background: #e5e7eb; overflow: hidden; }
.confidence-track span { display: block; height: 100%; background: linear-gradient(90deg, var(--accent), #8b5cf6); }
.confidence-value { text-align: right; font-weight: 700; color: var(--accent); font-variant-numeric: tabular-nums; }
@media (max-width: 560px) {
  .suggestion-card { grid-template-columns: 34px minmax(0, 1fr); }
  .confidence-value { grid-column: 2; text-align: left; }
}

/* ===== copy-to-spreadsheet result ===== */
.sheet-bar { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; margin-bottom: 12px; }
.copy-hint { color: var(--muted); font-size: 12.5px; }
.sheet-table th { background: var(--accent-soft); color: var(--accent); white-space: nowrap; }
.sheet-table td { white-space: nowrap; }
.sheet-table td .badge { font-weight: 600; }
details.more { margin-top: 16px; }
details.more > summary {
  cursor: pointer; font-size: 13.5px; font-weight: 600; color: var(--accent);
  padding: 8px 0; user-select: none; list-style: none;
}
details.more > summary::-webkit-details-marker { display: none; }
details.more > summary::before { content: '▸ '; }
details.more[open] > summary::before { content: '▾ '; }

/* ===== template builder ===== */
.field-row {
  display: grid; grid-template-columns: 1.1fr 1.6fr .8fr auto auto; gap: 10px;
  align-items: start; padding: 12px; border: 1px solid var(--line); border-radius: 12px;
  margin-bottom: 10px; background: #fbfcff;
}
.field-row input, .field-row select { margin-top: 0; }
.field-row .rm { color: var(--err); background: none; border: none; font-size: 17px; cursor: pointer; padding: 9px 6px; }
.field-row .ml { display: flex; align-items: center; gap: 6px; font-size: 12.5px; color: var(--muted); font-weight: 500; padding-top: 10px; white-space: nowrap; }
.field-row .ml input { width: auto; margin: 0; }
@media (max-width: 900px) { .field-row { grid-template-columns: 1fr; } }
.hint { color: var(--muted); font-size: 12.5px; margin-top: 4px; font-weight: 400; }

.tpl-card { display: flex; flex-direction: column; gap: 8px; }
.tpl-card .tags { display: flex; flex-wrap: wrap; gap: 6px; }
.tpl-card h3 { font-size: 16px; }
.tpl-actions { display: flex; gap: 8px; margin-top: 8px; }

/* ===== key display ===== */
.key-box {
  background: #0f172a; color: #a5b4fc; border-radius: 12px; padding: 16px 18px;
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; font-size: 13.5px;
  display: flex; justify-content: space-between; align-items: center; gap: 12px;
  margin: 8px 0 14px; word-break: break-all;
}
.key-box .btn { flex-shrink: 0; }

/* ===== modal ===== */
.modal-bg {
  position: fixed; inset: 0; background: rgba(15, 20, 40, .55); z-index: 40;
  display: none; align-items: center; justify-content: center; padding: 20px;
  backdrop-filter: blur(2px);
}
.modal-bg.open { display: flex; }
.modal {
  background: #fff; border-radius: 16px; padding: 26px; width: 100%; max-width: 460px;
  box-shadow: 0 30px 70px -20px rgba(0, 0, 0, .4);
}
.modal h3 { margin-bottom: 16px; font-size: 17px; }

/* ===== docs ===== */
.docs h2 { margin: 26px 0 10px; font-size: 17px; }
.docs h2:first-child { margin-top: 0; }
.docs p { margin-bottom: 10px; color: #374151; }
.docs code.inline { background: var(--accent-soft); color: var(--accent); padding: 2px 7px; border-radius: 6px; font-size: 13px; }
.docs table.tbl { margin: 10px 0 18px; }

.pager { display: flex; gap: 8px; margin-top: 16px; align-items: center; }
.pager .cur { font-weight: 700; }

.empty { text-align: center; color: var(--muted); padding: 40px 10px; }
.empty .big { font-size: 38px; margin-bottom: 8px; }

/* ===== mobile app bar + hamburger (hidden on desktop) ===== */
.hamburger {
  display: none; align-items: center; justify-content: center;
  width: 40px; height: 40px; border-radius: 10px; border: 1px solid var(--line);
  background: #fff; font-size: 20px; cursor: pointer; color: var(--ink); flex-shrink: 0;
}
.hamburger:active { background: var(--accent-soft); }
.sb-overlay {
  position: fixed; inset: 0; background: rgba(15, 20, 40, .5); z-index: 25;
  opacity: 0; visibility: hidden; transition: opacity .2s;
}
.sb-overlay.show { opacity: 1; visibility: visible; }
.topbar-brand { display: none; align-items: center; gap: 9px; font-weight: 700; font-size: 16px; }
.topbar-brand .brand-logo { width: 32px; height: 32px; font-size: 11px; border-radius: 9px; }

/* ===== responsive: tablet ===== */
@media (max-width: 1024px) {
  .grid.cols-4 { grid-template-columns: repeat(2, 1fr); }
  .grid.cols-3 { grid-template-columns: repeat(2, 1fr); }
  .content { padding: 18px 22px 40px; }
  .topbar { padding: 18px 22px 0; }
}

/* ===== responsive: phone / small tablet — sidebar becomes a drawer ===== */
@media (max-width: 860px) {
  .shell { display: block; }
  .sidebar {
    position: fixed; top: 0; left: 0; height: 100dvh; width: 82%; max-width: 300px;
    z-index: 30; transform: translateX(-100%); transition: transform .25s ease;
    box-shadow: 0 0 40px rgba(0, 0, 0, .3);
  }
  .sidebar.open { transform: translateX(0); }
  .main { min-height: 100dvh; }
  .topbar {
    position: sticky; top: 0; z-index: 20; display: flex; align-items: center; gap: 12px;
    padding: 12px 16px; background: rgba(255, 255, 255, .92);
    backdrop-filter: saturate(1.5) blur(8px); border-bottom: 1px solid var(--line);
  }
  .hamburger { display: inline-flex; }
  .topbar h1 { font-size: 18px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
  .topbar-brand { display: flex; margin-left: auto; }
  .content { padding: 16px 16px 90px; }

  .grid.cols-4, .grid.cols-3, .grid.cols-2 { grid-template-columns: 1fr; }
  .card { padding: 16px 16px; border-radius: 12px; }
  .stat .num { font-size: 26px; }

  /* forms stack; action clusters wrap */
  form.grid.cols-4 { grid-template-columns: 1fr; }
  .field-row { grid-template-columns: 1fr; }

  /* wide tables scroll inside their wrapper instead of blowing out the page */
  .tbl-wrap { overflow-x: auto; -webkit-overflow-scrolling: touch; }
  .tbl-wrap table.tbl { min-width: 560px; }

  .key-box { flex-direction: column; align-items: stretch; gap: 8px; }
  .key-box .btn { align-self: flex-end; }
  pre.json { font-size: 12px; padding: 14px; }

  /* comfortable tap targets */
  .btn { padding: 10px 16px; }
  .btn.sm { padding: 8px 12px; }
  .sidebar nav a { padding: 12px 12px; font-size: 15px; }
}

@media (max-width: 420px) {
  .topbar h1 { font-size: 16px; }
  .content { padding: 14px 12px 90px; }
  .login-card { padding: 28px 22px 24px; }
}

/* respect users who prefer reduced motion */
@media (prefers-reduced-motion: reduce) {
  .sidebar, .sb-overlay, .btn, .dropzone { transition: none; }
}

/* ===== realtime system monitor ===== */
.monitor-page { display: grid; gap: 16px; }
.monitor-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; flex-wrap: wrap; }
.monitor-stats .stat .ic { width: auto; min-width: 42px; padding: 0 9px; font-size: 11px; }
.monitor-chart-card { min-width: 0; }
.monitor-chart-card canvas { width: 100%; height: 280px; display: block; }
.chart-legend { display: flex; gap: 18px; flex-wrap: wrap; margin: -4px 0 10px; color: var(--muted); font-size: 12px; }
.chart-legend span::before { content: ''; display: inline-block; width: 18px; height: 3px; margin: 0 6px 3px 0; border-radius: 2px; }
.chart-legend .cpu::before { background: #4f46e5; } .chart-legend .io::before { background: #d97706; }
.chart-legend .ram::before { background: #059669; } .chart-legend .gpu::before { background: #9333ea; }
.monitor-detail-grid .card { min-width: 0; }
.monitor-detail-grid .kv-table td:first-child { width: 155px; }
.monitor-detail-grid .kv-table td:last-child { overflow-wrap: anywhere; }

/* ===== Vietnamese handwriting training ===== */
.training-page { display: grid; gap: 16px; }
.training-intro { display: flex; justify-content: space-between; align-items: center; gap: 24px; }
.training-intro h2 { margin-bottom: 6px; }
.training-intro p { color: #374151; }
.training-stats .card { color: var(--ink); }
.training-stats .selected { border-color: #a5b4fc; box-shadow: 0 0 0 2px rgba(79, 70, 229, .09), var(--shadow); }
.training-grid { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 16px; }
.training-sample { min-width: 0; display: flex; flex-direction: column; }
.training-image {
  height: 155px; display: flex; align-items: center; justify-content: center; overflow: hidden;
  margin: -8px -10px 12px; padding: 10px; border-radius: 10px; background: #f8fafc;
  border: 1px solid var(--line);
}
.training-image img { max-width: 100%; max-height: 100%; object-fit: contain; image-rendering: auto; }
.training-meta { display: flex; justify-content: space-between; gap: 8px; color: var(--muted); font-size: 11.5px; margin-bottom: 8px; }
.training-meta span:first-child { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.training-sample label { margin-bottom: 8px; }
.training-sample input, .training-sample select { margin-top: 4px; }
.training-savebar {
  position: sticky; bottom: 14px; z-index: 10; display: flex; align-items: center; gap: 14px;
  margin-top: 18px; padding: 12px 14px; border: 1px solid var(--line); border-radius: 12px;
  background: rgba(255, 255, 255, .94); box-shadow: 0 10px 30px -12px rgba(22, 25, 43, .35);
  backdrop-filter: blur(8px);
}
@media (max-width: 1200px) { .training-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); } }
@media (max-width: 700px) {
  .training-intro { align-items: stretch; flex-direction: column; }
  .training-grid { grid-template-columns: 1fr; }
  .training-savebar { align-items: stretch; flex-direction: column; }
}
