/* ════════════════════════════════════════════════════════════════
   MDLab Viewer — app shell layout
   Builds on brand.css tokens (Intralab IO design system).
   ════════════════════════════════════════════════════════════════ */

/* ── Dark theme token overrides ─────────────────────────────── */
:root {
  --topbar-h: 56px;
  --sidebar-w: 290px;
  --surface:        var(--white);
  --surface-2:      var(--gray-50);
  --surface-3:      var(--gray-100);
  --border-c:       var(--gray-200);
  --text-strong:    var(--blue-dark);
  --text-1:         var(--gray-700);
  --text-2:         var(--gray-500);
  --topbar-bg:      var(--blue-dark);
}
[data-theme="dark"] {
  --surface:        #0f1b29;
  --surface-2:      #132436;
  --surface-3:      #1b3148;
  --border-c:       #24405e;
  --text-strong:    #e8f2fb;
  --text-1:         #cbd9e8;
  --text-2:         #8aa6c2;
  --blue-pale:      #16304a;
  --topbar-bg:      #0b1521;
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  font-family: var(--font-display);
  background: var(--surface-2);
  color: var(--text-1);
  overflow: hidden;
}

/* ── App grid ───────────────────────────────────────────────── */
.app {
  display: grid;
  grid-template-rows: calc(var(--topbar-h) + env(safe-area-inset-top, 0px)) 1fr;
  height: 100vh;
  height: 100dvh;          /* iOS: account for the dynamic toolbar */
}

/* ── Topbar ─────────────────────────────────────────────────── */
.topbar {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  padding: env(safe-area-inset-top, 0px) max(var(--space-3), env(safe-area-inset-right, 0px)) 0 max(var(--space-3), env(safe-area-inset-left, 0px));
  background: var(--topbar-bg);
  color: #fff;
  box-shadow: var(--shadow-sm);
  z-index: 50;
}
.topbar .brand { display: flex; align-items: center; gap: 10px; margin-right: var(--space-3); }
.topbar .brand img { height: 30px; width: auto; display: block; }
.topbar .spacer { flex: 1; }
.topbar .group { display: flex; align-items: center; gap: 4px; }
.topbar .divider { width: 1px; height: 26px; background: rgba(255,255,255,.16); margin: 0 6px; }

.tb-btn {
  display: inline-flex; align-items: center; gap: 7px;
  height: 34px; padding: 0 12px;
  border: 1px solid transparent;
  border-radius: var(--radius-sm);
  background: transparent;
  color: rgba(255,255,255,.86);
  font-family: var(--font-display);
  font-size: var(--text-sm); font-weight: 600;
  cursor: pointer; white-space: nowrap;
  transition: background .15s, color .15s, border-color .15s;
}
.tb-btn:hover { background: rgba(255,255,255,.10); color: #fff; }
.tb-btn:active { transform: scale(.97); }
.tb-btn.primary { background: var(--accent); color: #06212b; }
.tb-btn.primary:hover { background: #2cc6e6; }
.tb-btn.active { background: rgba(255,255,255,.16); color: #fff; border-color: rgba(255,255,255,.18); }
.tb-btn:disabled { opacity: .4; cursor: not-allowed; }
.tb-btn svg { width: 17px; height: 17px; stroke: currentColor; fill: none; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
.tb-btn .label-sm { font-size: var(--text-xs); }
@media (max-width: 880px) { .tb-btn .hide-sm { display: none; } }

/* ── Main split: sidebar | content ──────────────────────────── */
.main { display: grid; grid-template-columns: var(--sidebar-w) 1fr; min-height: 0; }
.main.sidebar-collapsed { grid-template-columns: 0 1fr; }

.sidebar {
  background: var(--surface);
  border-right: 1px solid var(--border-c);
  display: flex; flex-direction: column;
  min-height: 0; overflow: hidden;
}
.sidebar-tabs { display: flex; border-bottom: 1px solid var(--border-c); }
.sidebar-tab {
  flex: 1; padding: 10px 8px;
  background: transparent; border: none; cursor: pointer;
  font-family: var(--font-mono); font-size: var(--text-xs); font-weight: 700;
  text-transform: uppercase; letter-spacing: 1px;
  color: var(--text-2); border-bottom: 2px solid transparent;
}
.sidebar-tab.active { color: var(--blue-primary); border-bottom-color: var(--blue-primary); }
.sidebar-panel { flex: 1; overflow: auto; padding: var(--space-3); display: none; }
.sidebar-panel.active { display: block; }

/* Outline (TOC) */
.toc { list-style: none; margin: 0; padding: 0; }
.toc li { margin: 0; }
.toc a {
  display: block; padding: 5px 10px; border-radius: var(--radius-xs);
  color: var(--text-2); font-size: var(--text-sm); line-height: 1.4;
  border-left: 2px solid transparent; text-decoration: none;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.toc a:hover { background: var(--blue-pale); color: var(--blue-primary); }
.toc a.active { color: var(--blue-primary); border-left-color: var(--blue-primary); background: var(--blue-pale); font-weight: 600; }
.toc .lvl-1 { padding-left: 0; font-weight: 600; }
.toc .lvl-2 a { padding-left: 18px; }
.toc .lvl-3 a { padding-left: 32px; }
.toc .lvl-4 a { padding-left: 46px; }
.toc .lvl-5 a { padding-left: 60px; }
.toc .lvl-6 a { padding-left: 74px; }

/* File tree */
.tree { list-style: none; margin: 0; padding: 0; font-size: var(--text-sm); }
.tree ul { list-style: none; margin: 0; padding-left: 14px; }
.tree-item {
  display: flex; align-items: center; gap: 6px;
  padding: 4px 8px; border-radius: var(--radius-xs); cursor: pointer;
  color: var(--text-1); user-select: none;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.tree-item:hover { background: var(--blue-pale); }
.tree-item.active { background: var(--blue-pale); color: var(--blue-primary); font-weight: 600; }
.tree-item svg { width: 15px; height: 15px; flex-shrink: 0; stroke: var(--text-2); fill: none; stroke-width: 2; }
.tree-item.is-dir > svg { stroke: var(--blue-primary); }

.panel-empty { color: var(--text-2); font-size: var(--text-sm); text-align: center; padding: 28px 14px; line-height: 1.6; }

/* ── Content area ───────────────────────────────────────────── */
.content { display: flex; flex-direction: column; min-width: 0; min-height: 0; background: var(--surface-2); }

/* Tab bar */
.tabbar {
  display: flex; align-items: stretch; gap: 0;
  background: var(--surface); border-bottom: 1px solid var(--border-c);
  overflow-x: auto; min-height: 40px; scrollbar-width: thin;
}
.tabbar:empty::after { content: ''; }
.tab {
  display: inline-flex; align-items: center; gap: 8px;
  padding: 0 10px 0 14px; height: 40px;
  border-right: 1px solid var(--border-c);
  background: transparent; cursor: pointer;
  font-size: var(--text-sm); color: var(--text-2);
  white-space: nowrap; max-width: 240px; flex-shrink: 0;
}
.tab:hover { background: var(--surface-2); color: var(--text-1); }
.tab.active { background: var(--surface-2); color: var(--text-strong); font-weight: 600; box-shadow: inset 0 -2px 0 var(--blue-primary); }
.tab .tab-name { overflow: hidden; text-overflow: ellipsis; }
.tab .dot { color: var(--accent); font-size: 18px; line-height: 0; }
.tab .tab-close {
  display: inline-flex; align-items: center; justify-content: center;
  width: 18px; height: 18px; border-radius: 4px; border: none;
  background: transparent; color: var(--text-2); cursor: pointer; font-size: 15px;
}
.tab .tab-close:hover { background: var(--gray-300); color: var(--blue-dark); }

/* Panes */
.panes { flex: 1; display: flex; min-height: 0; }
.panes.split .pane { flex: 1 1 50%; }
.panes:not(.split) .pane[data-pane="right"] { display: none; }
.pane {
  flex: 1; min-width: 0; min-height: 0; display: flex; flex-direction: column;
  position: relative; background: var(--surface-2);
}
.pane + .pane { border-left: 1px solid var(--border-c); }
.panes.split .pane.focused::before {
  content: ''; position: absolute; inset: 0; pointer-events: none;
  box-shadow: inset 0 0 0 2px var(--blue-lighter); z-index: 5;
}

/* Viewer */
.doc-scroll { flex: 1; overflow: auto; }
.doc-view {
  max-width: 900px; margin: 0 auto; padding: 40px 56px 120px;
  background: var(--surface); min-height: 100%;
}
.panes.split .doc-view { padding: 32px 36px 100px; }

/* Editor (CodeMirror + live preview) */
.editor-split { flex: 1; display: grid; grid-template-columns: 1fr 1fr; min-height: 0; }
.editor-split.preview-off { grid-template-columns: 1fr; }
.cm-host { min-width: 0; overflow: hidden; border-right: 1px solid var(--border-c); }
.cm-host .CodeMirror { height: 100%; font-family: var(--font-mono); font-size: 14px; }
.editor-preview { overflow: auto; background: var(--surface); }
.editor-preview .doc-view { padding: 28px 36px 100px; }

/* Empty / welcome state */
.welcome { flex: 1; display: flex; align-items: center; justify-content: center; padding: 32px; }
.welcome-card { width: 100%; max-width: 540px; text-align: center; }
.welcome-card img { height: 64px; margin: 0 auto var(--space-5); }
.welcome-card h1 { font-size: var(--text-2xl); color: var(--text-strong); margin-bottom: var(--space-2); }
.welcome-card p { color: var(--text-2); margin-bottom: var(--space-6); }
.welcome-actions { display: flex; gap: var(--space-3); justify-content: center; flex-wrap: wrap; }
.welcome-hint { margin-top: var(--space-6); font-family: var(--font-mono); font-size: var(--text-xs); color: var(--text-2); }
.kbd { font-family: var(--font-mono); font-size: .72rem; background: var(--surface-3); border: 1px solid var(--border-c); border-bottom-width: 2px; border-radius: 5px; padding: 1px 6px; color: var(--text-1); }

/* Drag & drop overlay */
.drop-overlay {
  position: fixed; inset: 0; z-index: 200; display: none;
  align-items: center; justify-content: center;
  background: rgba(26,58,92,.55); backdrop-filter: blur(3px);
}
.drop-overlay.show { display: flex; }
.drop-card {
  background: var(--surface); border: 3px dashed var(--blue-primary);
  border-radius: var(--radius-lg); padding: 48px 64px; text-align: center;
  color: var(--text-strong); font-weight: 700; font-size: var(--text-lg);
}

/* Toasts */
.toasts { position: fixed; bottom: 18px; right: 18px; z-index: 300; display: flex; flex-direction: column; gap: 8px; }
.toast {
  background: var(--blue-dark); color: #fff; padding: 11px 16px; border-radius: var(--radius-sm);
  box-shadow: var(--shadow-lg); font-size: var(--text-sm); max-width: 360px;
  animation: fadeInUp .25s ease; display: flex; align-items: center; gap: 9px;
}
.toast.error { background: var(--color-error); }
.toast.success { background: var(--color-success); }
.toast.warn { background: var(--color-warning); }

/* Busy spinner overlay (export) */
.busy { position: fixed; inset: 0; z-index: 250; display: none; align-items: center; justify-content: center; background: rgba(15,27,41,.45); }
.busy.show { display: flex; }
.busy .spin { width: 44px; height: 44px; border: 4px solid rgba(255,255,255,.3); border-top-color: #fff; border-radius: 50%; animation: spin 1s linear infinite; }
.busy .msg { color: #fff; margin-left: 14px; font-weight: 600; }

/* Scrollbars */
::-webkit-scrollbar { width: 11px; height: 11px; }
::-webkit-scrollbar-thumb { background: var(--gray-300); border-radius: 6px; border: 2px solid transparent; background-clip: padding-box; }
::-webkit-scrollbar-thumb:hover { background: var(--gray-400); background-clip: padding-box; border: 2px solid transparent; }
[data-theme="dark"] ::-webkit-scrollbar-thumb { background: #2b4a6b; background-clip: padding-box; }

/* ── Topbar: icon-only buttons, overflow menu ───────────────── */
.tb-btn.icon-only { padding: 0; width: 36px; justify-content: center; }
.tb-btn.icon-only svg { width: 19px; height: 19px; }
.more-wrap { position: relative; display: none; }
.more-menu {
  position: absolute; top: calc(100% + 6px); right: 0; z-index: 120;
  min-width: 210px; padding: 6px;
  background: var(--surface); color: var(--text-1);
  border: 1px solid var(--border-c); border-radius: var(--radius-md);
  box-shadow: var(--shadow-lg);
}
.more-menu button {
  display: flex; align-items: center; gap: 10px; width: 100%;
  padding: 10px 12px; border: none; background: transparent; cursor: pointer;
  font-family: var(--font-display); font-size: var(--text-base); font-weight: 600;
  color: var(--text-1); border-radius: var(--radius-sm); text-align: left;
}
.more-menu button:hover { background: var(--blue-pale); color: var(--blue-primary); }
.more-menu button:disabled { opacity: .4; cursor: not-allowed; }
.more-menu button svg { width: 18px; height: 18px; stroke: currentColor; fill: none; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; flex-shrink: 0; }
.more-menu .menu-sep { height: 1px; background: var(--border-c); margin: 5px 4px; }

/* Version badge */
.app-ver { font-family: var(--font-mono); font-size: 10px; font-weight: 700; color: rgba(255,255,255,.5); letter-spacing: .5px; margin-left: 2px; align-self: flex-end; padding-bottom: 4px; }

/* Tree loading / empty messages */
.tree-msg { list-style: none; color: var(--text-2); font-size: var(--text-xs); font-family: var(--font-mono); padding: 4px 10px; }
.tree-item.is-dir.open > svg { stroke: var(--blue-mid); }

/* Welcome footer links */
.welcome-foot { margin-top: var(--space-5); font-size: var(--text-sm); color: var(--text-2); }
.welcome-foot a { color: var(--blue-primary); }

/* Update banner toast */
.toast.update { background: var(--blue-mid); gap: 14px; }
.toast-btn { background: #fff; color: var(--blue-dark); border: none; border-radius: var(--radius-sm); padding: 5px 12px; font-weight: 700; font-size: var(--text-sm); cursor: pointer; font-family: var(--font-display); }

/* ── Sobre (About) modal ────────────────────────────────────── */
.modal.about { max-width: 720px; }
.about-hero { text-align: center; padding: 6px 0 18px; }
.about-logo { height: 56px; margin: 0 auto 12px; }
.about-tag { color: var(--text-2); font-size: var(--text-md); max-width: 440px; margin: 0 auto !important; }
.about-features { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.feat { display: flex; gap: 11px; align-items: flex-start; padding: 12px 14px; background: var(--surface-2); border: 1px solid var(--border-c); border-radius: var(--radius-md); }
.feat-ic { font-size: 20px; line-height: 1.2; flex-shrink: 0; }
.feat b { display: block; color: var(--text-strong); font-size: var(--text-base); }
.feat span { display: block; color: var(--text-2); font-size: var(--text-sm); line-height: 1.45; margin-top: 2px; }
.about-divider { height: 1px; background: var(--border-c); margin: 24px 0; }
.about-company { text-align: center; }
.about-intralab-logo { height: 40px; margin: 0 auto 8px; }
.about-tag2 { font-family: var(--font-mono); font-size: var(--text-sm); color: var(--blue-primary); margin: 0 0 14px !important; letter-spacing: .3px; }
.about-company p { color: var(--text-1); text-align: left; max-width: 560px; margin: 0 auto 14px; line-height: 1.6; }
.about-links { display: flex; flex-wrap: wrap; gap: 9px; justify-content: center; margin-bottom: 16px; }
.about-link { display: inline-flex; align-items: center; gap: 6px; padding: 8px 14px; background: var(--blue-pale); color: var(--blue-mid); border-radius: var(--radius-full); font-weight: 700; font-size: var(--text-sm); }
.about-link:hover { background: var(--blue-primary); color: #fff; }
.about-copy { font-size: var(--text-xs); color: var(--text-2); text-align: center !important; }
.about-version { display: inline-block; margin-top: 6px; font-family: var(--font-mono); font-size: var(--text-xs); font-weight: 700; color: var(--blue-primary); letter-spacing: .5px; }
.welcome-ver { font-family: var(--font-mono); font-size: var(--text-xs); color: var(--text-2); }

/* ── Mobile sidebar drawer + scrim ──────────────────────────── */
.scrim {
  position: fixed; inset: 0; z-index: 39; display: none;
  background: rgba(15,27,41,.5); backdrop-filter: blur(2px);
}
.app.drawer-open .scrim { display: block; }

/* ── Instruções modal ───────────────────────────────────────── */
.modal-overlay {
  position: fixed; inset: 0; z-index: 400; display: flex;
  align-items: center; justify-content: center; padding: 20px;
  background: rgba(15,27,41,.55); backdrop-filter: blur(3px);
}
.modal-overlay[hidden] { display: none; }
.modal {
  width: 100%; max-width: 680px; max-height: 88vh; display: flex; flex-direction: column;
  background: var(--surface); color: var(--text-1);
  border-radius: var(--radius-lg); box-shadow: var(--shadow-xl); overflow: hidden;
  animation: fadeInUp .22s ease;
}
.modal-head { display: flex; align-items: center; justify-content: space-between; padding: 16px 20px; border-bottom: 1px solid var(--border-c); }
.modal-head h2 { font-size: var(--text-xl); color: var(--text-strong); margin: 0; }
.modal-close { width: 34px; height: 34px; border: none; background: var(--surface-3); border-radius: var(--radius-sm); font-size: 22px; line-height: 1; color: var(--text-2); cursor: pointer; }
.modal-close:hover { background: var(--gray-300); color: var(--blue-dark); }
.modal-tabs { display: flex; gap: 2px; padding: 8px 14px 0; border-bottom: 1px solid var(--border-c); overflow-x: auto; }
.modal-tab { padding: 9px 14px; border: none; background: transparent; cursor: pointer; white-space: nowrap;
  font-family: var(--font-display); font-size: var(--text-sm); font-weight: 700; color: var(--text-2); border-bottom: 2px solid transparent; }
.modal-tab.active { color: var(--blue-primary); border-bottom-color: var(--blue-primary); }
.modal-body { padding: 20px 22px; overflow: auto; }
.modal-foot { padding: 12px 22px; border-top: 1px solid var(--border-c); text-align: center; }
.modal-foot a { color: var(--blue-primary); font-weight: 700; font-size: var(--text-sm); }
.help-pane { display: none; }
.help-pane.active { display: block; }
.help-pane h3 { font-size: var(--text-md); color: var(--text-strong); margin: 18px 0 8px; }
.help-pane p { color: var(--text-1); margin: 0 0 10px; line-height: 1.6; }
.help-pane ol, .help-pane ul { margin: 0 0 12px; padding-left: 22px; color: var(--text-1); line-height: 1.7; }
.help-pane li { margin: 3px 0; }
.help-pane code { font-family: var(--font-mono); font-size: .85em; background: var(--surface-3); color: var(--blue-mid); padding: .12em .4em; border-radius: 5px; }
.help-note { font-size: var(--text-sm); color: var(--text-2); background: var(--surface-2); border-left: 3px solid var(--blue-lighter); padding: 9px 12px; border-radius: 0 8px 8px 0; }
.help-keys { list-style: none; padding: 0 !important; }
.help-keys li { margin: 7px 0; color: var(--text-1); }

/* ════════════════════════════════════════════════════════════════
   RESPONSIVE
   ════════════════════════════════════════════════════════════════ */
@media (max-width: 860px) {
  .topbar .desktop-tools { display: none; }
  .more-wrap { display: block; }
  .topbar .divider.desktop { display: none; }
}

@media (max-width: 720px) {
  :root { --topbar-h: 52px; }
  .topbar { gap: 4px; padding: env(safe-area-inset-top, 0px) max(8px, env(safe-area-inset-right, 0px)) 0 max(8px, env(safe-area-inset-left, 0px)); }
  .topbar .brand img { height: 26px; }
  .tb-btn { height: 34px; }

  /* Sidebar becomes an overlay drawer */
  .main { grid-template-columns: 1fr !important; }
  .sidebar {
    position: fixed; top: calc(var(--topbar-h) + env(safe-area-inset-top, 0px)); left: 0; bottom: 0; z-index: 40;
    width: 86vw; max-width: 340px;
    transform: translateX(-100%); transition: transform .25s var(--ease);
    box-shadow: var(--shadow-xl);
  }
  .app.drawer-open .sidebar { transform: none; }

  /* Roomier reading on small screens */
  .doc-view { padding: 22px 18px 90px; }
  .panes.split .doc-view { padding: 18px 14px 80px; }

  /* Split stacks vertically; editor shows code only (preview via toggle/visualizar) */
  .panes.split { flex-direction: column; }
  .pane + .pane { border-left: none; border-top: 1px solid var(--border-c); }
  .editor-split { grid-template-columns: 1fr; }
  .editor-split .editor-preview { display: none; }

  .tab { max-width: 60vw; }
  .modal-tab { font-size: var(--text-xs); padding: 9px 9px; }
  .modal-body { padding: 16px; }

  .about-features { grid-template-columns: 1fr; }
  .app-ver { display: none; }
  .welcome { padding: 20px 16px; }
  .welcome-card h1 { font-size: var(--text-xl); }
  .welcome-card img { height: 52px; }
  .welcome-actions .btn { flex: 1 1 auto; }
}

@media (max-width: 420px) {
  .topbar .brand img { height: 22px; }
  .tb-btn.primary .hide-sm { display: none; }
}
