:root {
  --bg: #f5f6f8;
  --panel: #ffffff;
  --border: #e2e5ea;
  --text: #1c2129;
  --text-muted: #6b7280;
  --primary: #2563eb;
  --primary-hover: #1d4ed8;
  --danger: #dc2626;
  --danger-hover: #b91c1c;
  --success: #16a34a;
  --radius: 8px;
  --shadow: 0 1px 2px rgba(16, 24, 40, 0.06);
  font-family: -apple-system, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
}

* { box-sizing: border-box; }

/* The [hidden] attribute must always win, even over display rules below. */
[hidden] { display: none !important; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-size: 14px;
}

#app-shell {
  display: flex;
  min-height: 100vh;
}

/* ---------- Sidebar nav ---------- */
#sidebar {
  width: 210px;
  flex-shrink: 0;
  background: var(--panel);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  padding: 16px 0;
}
#sidebar .brand {
  font-weight: 700;
  font-size: 16px;
  padding: 0 20px 16px;
  border-bottom: 1px solid var(--border);
  margin-bottom: 8px;
}
#sidebar nav a {
  display: block;
  padding: 10px 20px;
  color: var(--text-muted);
  text-decoration: none;
  font-weight: 500;
  border-left: 3px solid transparent;
}
#sidebar nav a:hover { background: var(--bg); color: var(--text); }
#sidebar nav a.active {
  color: var(--primary);
  border-left-color: var(--primary);
  background: #eef2ff;
}

#sidebar-backdrop {
  display: none;
  position: fixed; inset: 0; background: rgba(15, 23, 42, 0.45);
  z-index: 120;
}
#sidebar-backdrop.visible { display: block; }

#btn-mobile-menu { display: none; padding: 6px 10px; font-size: 16px; line-height: 1; }

/* Desktop-only sidebar collapse (distinct from the mobile off-canvas drawer
   below, which already hides the sidebar by default on small screens). */
#btn-collapse-sidebar { padding: 6px 10px; font-size: 14px; line-height: 1; }
#app-shell.sidebar-collapsed #sidebar { width: 0; padding: 0; border: none; overflow: hidden; }

/* ---------- Main column ---------- */
#main-col { flex: 1; display: flex; flex-direction: column; min-width: 0; }

#topbar {
  height: 56px;
  border-bottom: 1px solid var(--border);
  background: var(--panel);
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 20px;
  flex-shrink: 0;
}
#topbar .topbar-left { display: flex; align-items: center; gap: 10px; }
#topbar .topbar-right { display: flex; align-items: center; gap: 10px; }
/* Encrypted/Decrypted session button (topbar-right, left of profile).
   Default: blue/primary ("Encrypted"). Unlocked: amber warning color, same
   tone as .badge.amber elsewhere, with a live countdown as its label. */
#btn-encryption-session { font-size: 13px; border-color: var(--primary); color: var(--primary); background: #fff; }
#btn-encryption-session:hover { background: #eef2ff; }
#btn-encryption-session.unlocked { background: #d97706; border-color: #d97706; color: #fff; }
#btn-encryption-session.unlocked:hover { background: #b45309; }
/* Hovering while unlocked offers to EXTEND the session (green). */
#btn-encryption-session.unlocked.extend,
#btn-encryption-session.unlocked.extend:hover { background: var(--success); border-color: var(--success); color: #fff; }
#topbar .mock-banner {
  font-size: 12px;
  color: #92400e;
  background: #fef3c7;
  padding: 3px 8px;
  border-radius: 4px;
  font-weight: 600;
}
#btn-tour {
  font-size: 12px;
  padding: 5px 10px;
  border-color: var(--primary);
  color: var(--primary);
  background: #fff;
}
#btn-tour:hover { background: #eef2ff; }
#user-menu-btn { font-size: 13px; }

#page-content { flex: 1; padding: 24px; overflow: auto; }

h1 { font-size: 20px; margin: 0 0 4px; }
h2 { font-size: 16px; margin: 0 0 12px; }
.page-header { display: flex; justify-content: space-between; align-items: flex-start; margin-bottom: 16px; flex-wrap: wrap; gap: 12px; }
.page-desc { color: var(--text-muted); font-size: 13px; margin: 0; }

.panel {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 16px;
}

.no-access {
  padding: 40px;
  text-align: center;
  color: var(--text-muted);
}

/* ---------- Buttons / inputs ---------- */
button, .btn {
  font-family: inherit;
  font-size: 13px;
  font-weight: 600;
  padding: 7px 14px;
  border-radius: 6px;
  border: 1px solid var(--border);
  background: var(--panel);
  color: var(--text);
  cursor: pointer;
  display: inline-block;
  text-decoration: none;
}
button:hover { background: var(--bg); }
button.primary { background: var(--primary); border-color: var(--primary); color: #fff; }
button.primary:hover { background: var(--primary-hover); }
button.danger { background: #fff; border-color: var(--danger); color: var(--danger); }
button.danger:hover { background: var(--danger); color: #fff; }
button.subtle { border-color: transparent; background: transparent; padding: 4px 8px; }
button:disabled { opacity: 0.5; cursor: not-allowed; }
button.small { padding: 4px 8px; font-size: 12px; }

input, select, textarea {
  font-family: inherit;
  font-size: 13px;
  padding: 6px 8px;
  border-radius: 6px;
  border: 1px solid var(--border);
  background: #fff;
  color: var(--text);
}
textarea { resize: vertical; }
label { font-size: 12px; font-weight: 600; color: var(--text-muted); display: block; margin-bottom: 4px; }
.field-row { margin-bottom: 12px; }
.field-row input, .field-row select, .field-row textarea { width: 100%; }
/* Checkboxes/radios must never stretch to the row width — that's what pushed
   them out of line with their labels. */
.field-row input[type="checkbox"], .field-row input[type="radio"],
.checkbox-row input[type="checkbox"], .checkbox-row input[type="radio"] { width: auto; margin: 0; flex-shrink: 0; }
.checkbox-row { display: flex; align-items: center; gap: 6px; }
label.checkbox-row { margin-bottom: 6px; font-weight: 400; font-size: 13px; color: var(--text); }
.checkbox-row label { margin: 0; }

/* Two-column asset form: fields are full width by default; half-width fields
   (Forms Designer "width" setting) sit two per row. Collapses on mobile. */
.form-grid { display: grid; grid-template-columns: 1fr 1fr; column-gap: 16px; }
.form-grid > .field-row { grid-column: 1 / -1; min-width: 0; }
.form-grid > .field-row.w-half { grid-column: auto; }

/* On/off style switch (e.g. Sheets' Formatted/Raw) — a real toggle, not tabs. */
.switch { display: inline-flex; align-items: center; gap: 8px; font-size: 13px; cursor: pointer; user-select: none; }
.switch .track {
  width: 34px; height: 18px; border-radius: 999px; background: var(--border);
  position: relative; transition: background 0.15s ease; flex-shrink: 0;
}
.switch .track::after {
  content: ''; position: absolute; top: 2px; left: 2px; width: 14px; height: 14px;
  border-radius: 50%; background: #fff; box-shadow: 0 1px 2px rgba(0,0,0,0.25);
  transition: left 0.15s ease;
}
.switch.on .track { background: var(--primary); }
.switch.on .track::after { left: 18px; }

/* Drag-and-drop field reordering (Forms Designer). */
.field-card[draggable="true"] { cursor: grab; }
.field-card.dragging { opacity: 0.5; }
.field-card.drag-over { border-top: 2px solid var(--primary); }
/* Checkbox rendered as a pill/chip toggle (Tags, "Group enabled", etc.) */
.checkbox-pill { border: 1px solid var(--border); border-radius: 999px; padding: 4px 10px; }
/* Hint text under a field, e.g. "Must be unique across all devices." */
.field-hint { font-size: 11px; margin-top: 3px; }
/* Compact note list inside a modal/panel */
.note-list { padding-left: 18px; font-size: 13px; }
/* Utilities: reused enough across pages to be worth a name, not enough to
   justify a per-purpose class. */
.text-right { text-align: right; }
.mb-20 { margin-bottom: 20px; }

/* ---------- Tables ---------- */
table { width: 100%; border-collapse: collapse; font-size: 13px; }
th, td { padding: 8px 10px; border-bottom: 1px solid var(--border); text-align: left; vertical-align: middle; }
th { color: var(--text-muted); font-weight: 600; font-size: 12px; text-transform: uppercase; letter-spacing: 0.02em; }
tr:hover td { background: #fafbfc; }

/* ---------- Sheets: actual grid look, distinct from the plain list on Assets ---------- */
/* Sheets loads all rows (no pagination) and the grid should fill the rest of
   the page's height, not just shrink to however many rows there are — makes
   #page-content a flex column for this page only (other pages are untouched)
   and lets .sheet-grid-wrap/.sheet-grid stretch into the remaining space. */
#page-content.sheet-page { display: flex; flex-direction: column; }
#page-content.sheet-page .sheet-grid-wrap { flex: 1; min-height: 0; }
#page-content.sheet-page .sheet-grid { height: 100%; }
.sheet-grid { border: 1px solid var(--border); border-radius: var(--radius); overflow: auto; }
.sheet-grid table { border-collapse: separate; border-spacing: 0; font-size: 13px; }
.sheet-grid th, .sheet-grid td {
  border-right: 1px solid var(--border);
  border-bottom: 1px solid var(--border);
  padding: 6px 10px;
  text-align: left;
  vertical-align: middle;
  white-space: nowrap;
}
.sheet-grid th {
  background: #f3f4f6;
  color: var(--text-muted);
  font-weight: 600;
  font-size: 12px;
  text-transform: uppercase;
  letter-spacing: 0.02em;
  position: sticky;
  top: 0;
  z-index: 2;
}
/* Row-number column: index only, not a field. "+" on the trailing draft row. */
.sheet-grid th.row-num-col, .sheet-grid td.row-num {
  width: 36px;
  min-width: 36px;
  text-align: center;
  background: #f3f4f6;
  color: var(--text-muted);
  font-variant-numeric: tabular-nums;
  position: sticky;
  left: 0;
  z-index: 1;
}
.sheet-grid th.row-num-col { z-index: 3; }
.sheet-grid tr:hover td { background: #fafbfc; }
.sheet-grid tr:hover td.row-num { background: #eef0f2; }
/* Trailing blank row: creates a new asset on commit — see initial-build.md */
.sheet-grid tr.sheet-new-row td { color: var(--text-muted); font-style: italic; }
.sheet-grid tr.sheet-new-row td.row-num { font-style: normal; }

/* ---------- Sheets grid cell states ---------- */
/* Selected: Excel/Sheets-style active-cell border. Not yet editing. */
td.cell-selected { outline: 2px solid var(--primary); outline-offset: -2px; position: relative; cursor: cell; }
/* Editing: selected cell replaced by its native input, still same outline. */
td.cell-editing { outline: 2px solid var(--primary); outline-offset: -2px; padding: 0; }
td.cell-editing input, td.cell-editing select { width: 100%; height: 100%; border: none; border-radius: 0; padding: 6px 10px; }
td.cell-readonly { color: var(--text-muted); cursor: default; }
tr.clickable { cursor: pointer; }

.badge {
  display: inline-block;
  padding: 2px 8px;
  border-radius: 999px;
  font-size: 11px;
  font-weight: 700;
  color: #fff;
}
.badge.gray { background: #6b7280; }
.badge.green { background: #16a34a; }
.badge.amber { background: #d97706; }
.badge.red { background: #dc2626; }
.badge.blue { background: #2563eb; }

.tag-chip {
  display: inline-block;
  padding: 2px 8px;
  border-radius: 999px;
  font-size: 11px;
  font-weight: 600;
  color: #fff;
  margin: 1px;
}

/* ---------- Tabs (asset type switcher, forms designer, asset drawer) ---------- */
.tab-strip { display: flex; gap: 4px; border-bottom: 1px solid var(--border); margin-bottom: 16px; flex-wrap: wrap; }
/* A nested/secondary tab-strip used as a plain toggle group (Raw/Formatted,
   rule Kind), not the page's primary section nav. */
.tab-strip.plain { margin: 0; border: none; }
.tab-strip button {
  border: none; border-bottom: 2px solid transparent; border-radius: 0;
  background: transparent; padding: 10px 16px; color: var(--text-muted); font-weight: 600;
}
.tab-strip button.active { color: var(--primary); border-bottom-color: var(--primary); }

/* ---------- Toolbar / filters ---------- */
.toolbar { display: flex; gap: 8px; align-items: center; margin-bottom: 12px; flex-wrap: wrap; }
.toolbar .spacer { flex: 1; }
.filter-chip {
  display: flex; align-items: center; gap: 6px; background: var(--bg); border: 1px solid var(--border);
  border-radius: 6px; padding: 4px 8px; font-size: 12px;
}
.filter-chip button { padding: 0 4px; border: none; background: none; font-size: 12px; }

/* ---------- Modal ---------- */
.modal-overlay {
  position: fixed; inset: 0; background: rgba(15, 23, 42, 0.45);
  display: flex; align-items: flex-start; justify-content: center;
  padding: 40px 20px; overflow: auto; z-index: 50;
}
.modal {
  background: #fff; border-radius: 10px; width: 100%; max-width: 560px;
  box-shadow: 0 20px 60px rgba(0,0,0,0.25);
}
.modal.wide { max-width: 800px; }
.modal-header { display: flex; justify-content: space-between; align-items: center; padding: 16px 20px; border-bottom: 1px solid var(--border); }
.modal-header h2 { margin: 0; }
.modal-body { padding: 20px; max-height: 70vh; overflow: auto; }
.modal-footer { padding: 14px 20px; border-top: 1px solid var(--border); display: flex; justify-content: flex-end; gap: 8px; }
.modal-footer .spacer { flex: 1; }

/* ---------- Column picker ---------- */
.col-picker { display: flex; flex-direction: column; gap: 4px; max-height: 260px; overflow: auto; }

/* ---------- Forms designer ---------- */
.designer-layout { display: flex; gap: 20px; align-items: flex-start; }
.designer-fields { flex: 1; min-width: 0; }
.designer-side { width: 300px; flex-shrink: 0; }
.field-list { display: flex; flex-direction: column; gap: 8px; }
.field-card {
  border: 1px solid var(--border); border-radius: 8px; padding: 10px 12px; background: #fff;
  display: flex; justify-content: space-between; align-items: center; gap: 8px;
}
.field-card .meta { display: flex; flex-direction: column; gap: 2px; }
.field-card .meta .type { font-size: 11px; color: var(--text-muted); }
.field-card .actions { display: flex; gap: 4px; }
/* Compact variant: a condition/action row's controls laid out inline instead
   of stacked (rule editor). */
.field-card .meta.inline { flex-direction: row; align-items: center; gap: 8px; flex-wrap: wrap; }

.phone-preview {
  width: 260px; border: 8px solid #1c2129; border-radius: 28px; background: #fff;
  padding: 16px 12px; margin: 0 auto; box-shadow: var(--shadow);
}
.phone-preview .pv-field { margin-bottom: 12px; }
.phone-preview .pv-label { font-size: 11px; font-weight: 700; color: var(--text-muted); margin-bottom: 3px; }
.phone-preview .pv-input { border: 1px solid var(--border); border-radius: 6px; padding: 6px 8px; font-size: 12px; color: var(--text-muted); }

/* ---------- Misc ---------- */
.muted { color: var(--text-muted); }
.pill-group { display: flex; gap: 6px; flex-wrap: wrap; }
.section-divider { border: none; border-top: 1px solid var(--border); margin: 8px 0 16px; }
.empty-state { text-align: center; padding: 40px 20px; color: var(--text-muted); }
.grid-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; }
.grid-3 { display: grid; grid-template-columns: repeat(3, 1fr); gap: 16px; }
a { color: var(--primary); }
.pagination { display: flex; gap: 8px; align-items: center; justify-content: flex-end; margin-top: 12px; font-size: 13px; }

/* ---------- QR scanner (mock) ---------- */
.qr-scanner { display: flex; flex-direction: column; align-items: center; }
.qr-scanner-frame {
  position: relative;
  width: 220px;
  height: 220px;
  border-radius: 16px;
  background: #111827;
  border: 3px solid var(--primary);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 48px;
  overflow: hidden;
}
.qr-scanline {
  position: absolute;
  left: 8px;
  right: 8px;
  height: 2px;
  background: #22d3ee;
  box-shadow: 0 0 8px 2px #22d3ee;
  animation: qr-scan-move 1.6s ease-in-out infinite;
}
@keyframes qr-scan-move {
  0% { top: 10px; }
  50% { top: 200px; }
  100% { top: 10px; }
}

/* ---------- Log entries (the Logs FIELD on asset forms) ---------- */
.log-entries { display: flex; flex-direction: column; border: 1px solid var(--border); border-radius: 8px; overflow: hidden; }
.log-entry {
  display: flex; align-items: baseline; gap: 8px; flex-wrap: wrap;
  padding: 8px 12px; font-size: 13px; border-bottom: 1px solid var(--border);
}
.log-entry:last-child { border-bottom: none; }
.log-entry:nth-child(odd) { background: var(--bg); }
.log-entry .log-when { color: var(--text-muted); font-size: 12px; white-space: nowrap; }
.log-entry .log-actor { font-weight: 600; white-space: nowrap; }
.log-entry .log-summary { flex: 1; min-width: 200px; }

/* Checkboxes inside table cells sit centered with their row text. */
td > input[type="checkbox"], th > input[type="checkbox"] { vertical-align: middle; margin: 0; }

/* ---------- Toasts ---------- */
#toast-root {
  position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%);
  display: flex; flex-direction: column; gap: 8px; z-index: 300; align-items: center;
}
.toast {
  background: #1c2129; color: #fff; padding: 10px 16px; border-radius: 8px;
  font-size: 13px; box-shadow: 0 8px 24px rgba(0,0,0,0.25);
  opacity: 0; transform: translateY(8px); transition: opacity 0.2s ease, transform 0.2s ease;
  max-width: min(90vw, 480px);
}
.toast.visible { opacity: 1; transform: translateY(0); }
.toast.error { background: var(--danger); }

/* ---------- Guided tour ---------- */
.tour-overlay {
  position: fixed; inset: 0; z-index: 200; background: transparent;
}
.tour-spotlight {
  position: fixed;
  border-radius: 10px;
  box-shadow: 0 0 0 9999px rgba(15, 23, 42, 0.68);
  pointer-events: none;
  transition: top 0.25s ease, left 0.25s ease, width 0.25s ease, height 0.25s ease, opacity 0.15s ease;
  border: 2px solid #fff;
  z-index: 201;
}
.tour-card {
  position: fixed;
  right: 24px;
  bottom: 24px;
  width: 340px;
  max-width: calc(100vw - 48px);
  background: #fff;
  border-radius: 12px;
  box-shadow: 0 20px 60px rgba(0,0,0,0.35);
  padding: 20px;
  z-index: 202;
}
.tour-card .tour-eyebrow {
  font-size: 11px; font-weight: 700; letter-spacing: 0.04em; text-transform: uppercase;
  color: var(--primary); margin-bottom: 6px;
}
.tour-card h3 { margin: 0 0 8px; font-size: 16px; }
.tour-card p { margin: 0 0 16px; font-size: 13px; color: var(--text-muted); line-height: 1.5; }
.tour-progress { display: flex; gap: 6px; margin-bottom: 4px; }
.tour-progress .dot { width: 6px; height: 6px; border-radius: 999px; background: var(--border); }
.tour-progress .dot.active { background: var(--primary); }
.tour-actions { display: flex; align-items: center; gap: 8px; }
.tour-actions .spacer { flex: 1; }

/* ======================================================================
   Mobile layout (phones/small tablets)
   ====================================================================== */
@media (max-width: 860px) {
  #btn-mobile-menu { display: inline-flex; align-items: center; justify-content: center; }
  #btn-collapse-sidebar { display: none; } /* mobile already hides the sidebar by default */

  #sidebar {
    position: fixed;
    top: 0; left: 0; bottom: 0;
    width: 240px;
    z-index: 130;
    transform: translateX(-100%);
    transition: transform 0.2s ease;
    box-shadow: 2px 0 12px rgba(0,0,0,0.15);
  }
  #sidebar.open { transform: translateX(0); }

  #topbar {
    height: auto;
    min-height: 56px;
    flex-wrap: wrap;
    gap: 8px;
    padding: 10px 12px;
  }
  #topbar .topbar-left { flex-wrap: wrap; gap: 6px; }
  #topbar .mock-banner { font-size: 10px; padding: 3px 6px; }
  #btn-tour { font-size: 11px; padding: 5px 8px; }

  #page-content { padding: 14px; }

  h1 { font-size: 18px; }
  .page-header { flex-direction: column; align-items: stretch; }
  .page-header button { width: 100%; }

  /* Tables scroll horizontally instead of squeezing columns unreadably */
  .panel { padding: 12px; overflow-x: auto; }
  table { min-width: 640px; }

  .toolbar button, .toolbar .btn { flex: 1 1 auto; }

  /* Tabs scroll horizontally rather than wrapping to many rows */
  .tab-strip { flex-wrap: nowrap; overflow-x: auto; -webkit-overflow-scrolling: touch; }
  .tab-strip button { flex-shrink: 0; }

  .grid-2, .grid-3 { grid-template-columns: 1fr; }

  .designer-layout { flex-direction: column; }
  .designer-side { width: 100%; }
  .phone-preview { width: 100%; max-width: 280px; }

  /* Modals fill the viewport width with slim margins */
  .modal-overlay { padding: 0; align-items: stretch; }
  .modal, .modal.wide { max-width: 100%; width: 100%; min-height: 100%; border-radius: 0; }
  .modal-body { max-height: none; }

  .tour-card { left: 12px; right: 12px; bottom: 12px; width: auto; max-width: none; }

  .pagination { flex-wrap: wrap; justify-content: center; }
}

@media (max-width: 480px) {
  #topbar .mock-banner { display: none; }
  .grid-3 { grid-template-columns: 1fr; }
}
