/* Shared components: buttons, inputs, cards, menus, dialogs, toasts, badges */

/* ── Buttons ────────────────────────────────────────────── */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 7px 14px;
  border-radius: var(--r-md);
  font-size: var(--text-sm);
  font-weight: 550;
  border: 1px solid transparent;
  transition: background var(--dur) var(--ease), border-color var(--dur) var(--ease);
  white-space: nowrap;
}
.btn-primary { background: var(--accent); color: var(--accent-fg); }
.btn-primary:hover { background: var(--accent-hover); }
.btn-secondary { background: var(--surface); border-color: var(--border-strong); color: var(--text); }
.btn-secondary:hover { background: var(--surface-hover); }
.btn-ghost { color: var(--text-muted); }
.btn-ghost:hover { background: var(--surface-hover); color: var(--text); }
.btn-danger { background: var(--danger); color: #fff; }
.btn-danger:hover { filter: brightness(1.1); }
.btn-danger-outline { border-color: var(--danger); color: var(--danger); background: transparent; }
.btn-danger-outline:hover { background: var(--danger-soft); }
.btn-sm { padding: 4px 10px; font-size: var(--text-xs); border-radius: var(--r-sm); }
.btn-icon { width: 34px; height: 34px; padding: 0; border-radius: var(--r-md); font-size: 1.05rem; flex-shrink: 0; }
.btn-block { width: 100%; }

/* ── Inputs ─────────────────────────────────────────────── */
.input, .select, .textarea {
  width: 100%;
  padding: 8px 12px;
  background: var(--bg);
  border: 1px solid var(--border-strong);
  border-radius: var(--r-md);
  font-size: var(--text-sm);
  transition: border-color var(--dur) var(--ease);
}
.input:focus, .select:focus, .textarea:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-soft);
}
.textarea { line-height: 1.5; min-height: 5.5em; }
.select { appearance: none; background-image: linear-gradient(45deg, transparent 50%, var(--text-muted) 50%), linear-gradient(135deg, var(--text-muted) 50%, transparent 50%); background-position: calc(100% - 18px) 55%, calc(100% - 13px) 55%; background-size: 5px 5px; background-repeat: no-repeat; padding-right: 34px; }

.field { margin-bottom: var(--sp-4); }
.field-label { display: block; font-size: var(--text-sm); font-weight: 600; margin-bottom: 6px; }
.field-help { font-size: var(--text-xs); color: var(--text-muted); margin-top: 5px; }

.search-input-wrap { position: relative; }
.search-input-wrap .bi-search {
  position: absolute; left: 11px; top: 50%; transform: translateY(-50%);
  color: var(--text-faint); font-size: .85rem; pointer-events: none;
}
.search-input-wrap .input { padding-left: 32px; }
.search-input-wrap .search-clear {
  position: absolute; right: 6px; top: 50%; transform: translateY(-50%);
  width: 24px; height: 24px; border-radius: var(--r-full);
  display: flex; align-items: center; justify-content: center;
  color: var(--text-faint);
}
.search-input-wrap .search-clear:hover { background: var(--surface-hover); color: var(--text); }

/* toggle switch */
.switch { position: relative; display: inline-flex; width: 40px; height: 22px; flex-shrink: 0; }
.switch input { opacity: 0; width: 0; height: 0; }
.switch .track {
  position: absolute; inset: 0; border-radius: var(--r-full);
  background: var(--border-strong); transition: background var(--dur) var(--ease);
}
.switch .track::before {
  content: ""; position: absolute; top: 3px; left: 3px;
  width: 16px; height: 16px; border-radius: var(--r-full);
  background: #fff; transition: transform var(--dur) var(--ease);
}
.switch input:checked + .track { background: var(--accent); }
.switch input:checked + .track::before { transform: translateX(18px); }
.switch input:focus-visible + .track { outline: 2px solid var(--accent); outline-offset: 2px; }

/* ── Cards ──────────────────────────────────────────────── */
.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--r-lg);
  padding: var(--sp-4);
}
.card-hover { transition: border-color var(--dur) var(--ease), transform var(--dur) var(--ease); }
.card-hover:hover { border-color: var(--border-strong); transform: translateY(-1px); }

/* ── Badges / chips ─────────────────────────────────────── */
.badge {
  display: inline-flex; align-items: center; gap: 4px;
  padding: 2px 8px;
  border-radius: var(--r-full);
  font-size: var(--text-xs);
  font-weight: 600;
  background: var(--surface-hover);
  color: var(--text-muted);
}
.badge-accent { background: var(--accent-soft); color: var(--accent); }
.badge-success { background: var(--success-soft); color: var(--success); }
.badge-warn { background: var(--warn-soft); color: var(--warn); }
.badge-danger { background: var(--danger-soft); color: var(--danger); }

/* ── Popover menus ──────────────────────────────────────── */
.menu-anchor { position: relative; }
.menu {
  position: absolute;
  top: calc(100% + 6px);
  left: 0;
  min-width: 230px;
  max-width: min(340px, calc(100vw - 24px));
  background: var(--bg-overlay);
  border: 1px solid var(--border);
  border-radius: var(--r-md);
  box-shadow: var(--shadow-2);
  padding: var(--sp-1);
  z-index: 200;
  /* grow with the window — only scroll when the viewport genuinely can't fit it */
  max-height: calc(100vh - 90px);
  max-height: min(88vh, calc(100dvh - 90px));
  overflow-y: auto;
}
.menu--right { left: auto; right: 0; }
.menu--up { top: auto; bottom: calc(100% + 6px); }
.menu-item {
  display: flex; align-items: center; gap: 10px;
  width: 100%;
  padding: 8px 10px;
  border-radius: var(--r-sm);
  font-size: var(--text-sm);
  color: var(--text);
  text-align: left;
}
.menu-item:hover { background: var(--surface-hover); text-decoration: none; color: var(--text); }
.menu-item.active { background: var(--accent-soft); color: var(--accent); }
.menu-item.danger { color: var(--danger); }
.menu-item.danger:hover { background: var(--danger-soft); }
.menu-item .bi { flex-shrink: 0; width: 1.1em; text-align: center; color: var(--text-muted); }
.menu-item.danger .bi { color: var(--danger); }
.menu-sep { border-top: 1px solid var(--border); margin: var(--sp-1) 0; }
.menu-header { padding: 8px 10px 6px; border-bottom: 1px solid var(--border); margin-bottom: var(--sp-1); }
.menu-header-name { font-weight: 650; font-size: var(--text-sm); }
.menu-header-sub { font-size: var(--text-xs); color: var(--text-muted); }
.menu-label {
  padding: 6px 10px 2px;
  font-size: var(--text-xs); font-weight: 650;
  text-transform: uppercase; letter-spacing: .05em;
  color: var(--text-faint);
}

/* ── Image lightbox ─────────────────────────────────────── */
dialog.lightbox {
  border: none;
  padding: 0;
  background: transparent;
  max-width: 96vw;
  max-height: 96vh;
  outline: none;
}
dialog.lightbox::backdrop { background: rgba(0, 0, 0, .82); }
dialog.lightbox img {
  display: block;
  max-width: 96vw;
  max-height: 96vh;
  border-radius: var(--r-md);
  cursor: zoom-out;
}

/* ── Dialogs (desktop modal / mobile bottom sheet) ─────── */
dialog.dlg {
  border: 1px solid var(--border);
  border-radius: var(--r-lg);
  background: var(--bg-overlay);
  color: var(--text);
  padding: 0;
  width: min(480px, calc(100vw - 32px));
  max-height: min(85vh, 85dvh);
  box-shadow: var(--shadow-2);
}
dialog.dlg::backdrop { background: var(--scrim); }
.dlg-body { padding: var(--sp-4); overflow-y: auto; max-height: calc(85dvh - 120px); }
.dlg-title { font-size: var(--text-lg); font-weight: 650; padding: var(--sp-4) var(--sp-4) 0; }
.dlg-actions {
  display: flex; justify-content: flex-end; gap: var(--sp-2);
  padding: var(--sp-3) var(--sp-4) var(--sp-4);
}

@media (max-width: 639px) {
  dialog.dlg {
    width: 100vw;
    max-width: 100vw;
    margin: auto 0 0;
    border-radius: var(--r-lg) var(--r-lg) 0 0;
    border-bottom: none;
    padding-bottom: env(safe-area-inset-bottom);
  }
  dialog.dlg::before {
    content: "";
    display: block;
    width: 42px; height: 4px;
    border-radius: var(--r-full);
    background: var(--border-strong);
    margin: 8px auto 0;
  }
}

/* ── Toasts ─────────────────────────────────────────────── */
.toast-region {
  position: fixed;
  bottom: calc(var(--sp-4) + env(safe-area-inset-bottom));
  left: 50%;
  transform: translateX(-50%);
  z-index: 500;
  display: flex;
  flex-direction: column;
  gap: var(--sp-2);
  align-items: center;
  pointer-events: none;
}
.toast {
  pointer-events: auto;
  display: flex; align-items: center; gap: 8px;
  background: var(--bg-overlay);
  border: 1px solid var(--border);
  border-radius: var(--r-md);
  box-shadow: var(--shadow-2);
  padding: 10px 14px;
  font-size: var(--text-sm);
  max-width: min(420px, calc(100vw - 32px));
  animation: toast-in 180ms var(--ease);
}
.toast--error { border-color: var(--danger); }
.toast--error .bi { color: var(--danger); }
.toast--success .bi { color: var(--success); }
@keyframes toast-in { from { opacity: 0; transform: translateY(8px); } }

/* ── Page scaffolding (ported pages) ────────────────────── */
.page-head {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: var(--sp-3);
  margin-bottom: var(--sp-4);
  flex-wrap: wrap;
}
.page-head h1 { margin-bottom: 2px; }
.page-head .page-sub { color: var(--text-muted); margin: 0; }

.toolbar {
  display: flex;
  align-items: center;
  gap: var(--sp-2);
  margin-bottom: var(--sp-3);
  flex-wrap: wrap;
}
.toolbar .search-input-wrap { max-width: 300px; flex: 1 1 200px; }
.toolbar-spacer { flex: 1; }

.data-table-wrap {
  overflow-x: auto;
  border: 1px solid var(--border);
  border-radius: var(--r-lg);
  background: var(--surface);
}
.data-table { width: 100%; border-collapse: collapse; font-size: var(--text-sm); }
.data-table th {
  text-align: left;
  font-weight: 650;
  color: var(--text-muted);
  padding: 10px 14px;
  border-bottom: 1px solid var(--border);
  white-space: nowrap;
  position: sticky;
  top: 0;
  background: var(--surface);
  z-index: 1;
}
.data-table td { padding: 9px 14px; border-bottom: 1px solid var(--border); vertical-align: middle; }
.data-table tbody tr:last-child td { border-bottom: none; }
.data-table tbody tr:hover { background: var(--surface-hover); }
.data-table tr.inactive { opacity: .55; }
.data-table tr.row-link { cursor: pointer; }
.data-table tr.row-link:hover .bi-chevron-right { color: var(--accent); }
.dt-actions { display: flex; gap: 2px; justify-content: flex-end; white-space: nowrap; }
.dt-num { text-align: right; font-variant-numeric: tabular-nums; }

.form-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 0 var(--sp-4); }
.form-grid .field-full { grid-column: 1 / -1; }
@media (max-width: 639px) { .form-grid { grid-template-columns: 1fr; } }

/* ── Banners ────────────────────────────────────────────── */
.app-banner {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px var(--sp-4);
  background: var(--accent-soft);
  color: var(--text);
  font-size: var(--text-sm);
  border-bottom: 1px solid var(--border);
}
.app-banner--warn { background: var(--warn-soft); }
.app-banner .bi { color: var(--accent); }
.app-banner--warn .bi { color: var(--warn); }
.app-banner-action { margin-left: auto; font-weight: 600; white-space: nowrap; }

/* non-interactive footer note inside menus */
.menu-note {
  padding: 8px 10px;
  font-size: var(--text-xs);
  color: var(--text-muted);
  border-top: 1px solid var(--border);
  margin-top: var(--sp-1);
  max-width: 260px;
}
.menu-note b { color: var(--warn); }

/* ── Slide-over panel (monitor drawer etc.) ─────────────── */
.slide-over {
  position: fixed;
  top: 0; right: 0; bottom: 0;
  width: min(520px, 100vw);
  background: var(--bg-raised);
  border-left: 1px solid var(--border);
  box-shadow: var(--shadow-2);
  z-index: 150;
  display: flex;
  flex-direction: column;
  transform: translateX(102%);
  transition: transform 200ms var(--ease);
}
.slide-over.open { transform: translateX(0); }
.slide-over-head {
  display: flex;
  align-items: center;
  gap: var(--sp-2);
  padding: var(--sp-3) var(--sp-4);
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}
.slide-over-body { flex: 1; min-height: 0; overflow-y: auto; padding: var(--sp-4); }
.slide-over-foot { flex-shrink: 0; border-top: 1px solid var(--border); padding: var(--sp-3) var(--sp-4); display: flex; gap: var(--sp-2); }

/* ── Tool requirements checklist panel ──────────────────── */
.req-panel {
  border: 1px solid var(--border);
  border-radius: var(--r-md);
  background: var(--bg);
  padding: var(--sp-3);
  margin-top: var(--sp-2);
}
.req-title { font-size: var(--text-xs); font-weight: 650; text-transform: uppercase; letter-spacing: .05em; color: var(--text-faint); margin-bottom: var(--sp-2); }
.req-section-label { font-size: var(--text-xs); font-weight: 600; color: var(--text-muted); margin: var(--sp-2) 0 4px; }
.req-optional { font-weight: 400; color: var(--text-faint); }
/* align-items:flex-start so a note that wraps to 2+ lines (e.g. the coding-
   sandbox read-only rows) grows the row cleanly instead of overlapping the
   next one; single-line rows look identical. */
.req-row { display: flex; align-items: flex-start; gap: 8px; margin-bottom: 6px; }
.req-status { flex-shrink: 0; width: 1em; text-align: center; font-size: .9rem; line-height: 1.5; }
.req-var { font-family: var(--font-mono); font-size: var(--text-sm); min-width: 90px; flex-shrink: 0; line-height: 1.5; word-break: break-all; }
/* min-width:0 lets the note wrap within the flex track instead of overflowing;
   it sits beside a possibly-long var name, so both need to share the row. */
.req-note { font-size: var(--text-xs); color: var(--text-muted); flex: 1; min-width: 0; line-height: 1.4; padding-top: 1px; }
.req-input { flex: 1; padding: 5px 10px; font-size: var(--text-sm); }
.req-inherited { background: var(--surface-hover); color: var(--text-muted); }
.req-status-line { margin-top: var(--sp-2); font-size: var(--text-sm); font-weight: 550; display: flex; align-items: center; gap: 6px; }
.req-status-line.ready { color: var(--success); }
.req-status-line.not-ready { color: var(--warn); }

/* ── Inline audio player (markdown.js enhanceAudioLinks) ── */
/* Inline video (tool 1471). Sits in the message bubble like the audio player;
   max-width keeps a portrait clip from dominating the column, and the download
   button is positioned over the corner rather than taking a row of its own. */
.video-player {
  position: relative;
  display: inline-block;
  max-width: min(480px, 100%);
  margin: 6px 0;
  vertical-align: middle;
  line-height: 0;
}
.video-player video {
  display: block;
  width: 100%;
  max-height: 70vh;          /* a tall 9:16 clip must not push the reply off screen */
  border: 1px solid var(--border);
  border-radius: var(--r-md);
  background: #000;          /* letterbox bars read as intentional, not as a gap */
}
.video-player .video-dl {
  position: absolute;
  top: 8px; right: 8px;
  width: 30px; height: 30px;
  display: flex; align-items: center; justify-content: center;
  border-radius: var(--r-full);
  background: rgba(0,0,0,.55);
  color: #fff;
  opacity: 0;
  transition: opacity .15s ease;
}
.video-player:hover .video-dl,
.video-player .video-dl:focus-visible { opacity: 1; }

.audio-player {
  display: inline-flex;
  align-items: center;
  gap: 10px;
  width: min(420px, 100%);
  padding: 8px 10px;
  margin: 4px 0;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--r-md);
  vertical-align: middle;
}
.audio-play {
  width: 34px; height: 34px;
  border-radius: var(--r-full);
  background: var(--accent);
  color: var(--accent-fg);
  display: flex; align-items: center; justify-content: center;
  font-size: 1.1rem;
  flex-shrink: 0;
  transition: background var(--dur) var(--ease);
}
.audio-play:hover { background: var(--accent-hover); }
.audio-main { flex: 1; min-width: 0; display: flex; flex-direction: column; gap: 5px; }
.audio-name {
  font-size: var(--text-xs);
  color: var(--text-muted);
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.audio-bar {
  display: block;
  height: 5px;
  border-radius: var(--r-full);
  background: var(--surface-hover);
  border: 1px solid var(--border);
  cursor: pointer;
  overflow: hidden;
}
.audio-bar-fill {
  display: block;
  height: 100%;
  width: 0%;
  border-radius: var(--r-full);
  background: var(--accent);
  transition: width 120ms linear;
}
.audio-time {
  font-size: var(--text-xs);
  color: var(--text-faint);
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
  flex-shrink: 0;
}
.audio-dl {
  width: 30px; height: 30px;
  border-radius: var(--r-sm);
  display: flex; align-items: center; justify-content: center;
  color: var(--text-muted);
  flex-shrink: 0;
}
.audio-dl:hover { background: var(--surface-hover); color: var(--text); text-decoration: none; }
.audio-player.audio-error { border-color: var(--danger); }

/* ── Leemo portal link pill (markdown.js enhanceLeemoLinks) ── */
.leemo-link {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  max-width: 100%;
  padding: 2px 10px;
  margin: 1px 0;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--r-full);
  font-size: var(--text-sm);
  font-weight: 550;
  color: var(--text);
  text-decoration: none;
  vertical-align: middle;
  transition: border-color var(--dur) var(--ease), background var(--dur) var(--ease);
}
.leemo-link:hover { border-color: var(--accent); background: var(--accent-soft); text-decoration: none; }
.leemo-link i { color: var(--accent); font-size: .95em; line-height: 1; }
.leemo-link .leemo-brand {
  color: var(--accent);
  font-weight: 700;
  font-size: var(--text-xs);
  letter-spacing: .04em;
  text-transform: uppercase;
  padding-right: 6px;
  border-right: 1px solid var(--border);
}
.leemo-link .leemo-label {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

/* ── Misc ───────────────────────────────────────────────── */
.spinner {
  width: 16px; height: 16px;
  border: 2px solid var(--border-strong);
  border-top-color: var(--accent);
  border-radius: var(--r-full);
  animation: spin .7s linear infinite;
  flex-shrink: 0;
}
@keyframes spin { to { transform: rotate(360deg); } }

.empty-state {
  text-align: center;
  color: var(--text-muted);
  padding: var(--sp-8) var(--sp-4);
}
.empty-state .bi { font-size: 2rem; color: var(--text-faint); display: block; margin-bottom: var(--sp-3); }

.avatar-img {
  border-radius: var(--r-full);
  object-fit: cover;
  background: var(--surface-hover);
  flex-shrink: 0;
}
.avatar-fallback {
  border-radius: var(--r-full);
  background: var(--accent-soft);
  color: var(--accent);
  display: flex; align-items: center; justify-content: center;
  flex-shrink: 0;
}

/* ── First-run guided tour (js/tour.js) ─────────────────────
   Fixed card, bottom-right, below slide-overs (150) and toasts (500) but above
   page content. On the chat page it sits above the composer. Collapsible to a
   pill; all colors via tokens so both themes work. */
.tour-card {
  position: fixed;
  right: var(--sp-4);
  bottom: var(--sp-4);
  z-index: 140;
  width: min(340px, calc(100vw - 24px));
  background: var(--bg-overlay);
  border: 1px solid var(--border);
  border-radius: var(--r-lg);
  box-shadow: var(--shadow-2);
  overflow: hidden;
  transition: opacity 0.7s var(--ease);
}
body.page-chat .tour-card { bottom: 96px; }        /* clear the composer */
.tour-card.tour-fade { opacity: 0; }
.tour-card .tour-pill { display: none; }
.tour-card.tour-collapsed .tour-body { display: none; }
.tour-card.tour-collapsed {
  width: auto;
  border-radius: var(--r-full);
}
.tour-card.tour-collapsed .tour-pill {
  display: inline-flex;
  align-items: center;
  gap: var(--sp-2);
  border: 0;
  background: none;
  color: var(--text);
  font: inherit;
  font-size: 0.85rem;
  padding: var(--sp-2) var(--sp-4);
  cursor: pointer;
}
.tour-card.tour-collapsed .tour-pill .bi { color: var(--accent); }
.tour-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: var(--sp-3) var(--sp-4);
  border-bottom: 1px solid var(--border);
}
.tour-title { font-weight: 600; font-size: 0.95rem; display: inline-flex; align-items: center; gap: var(--sp-2); }
.tour-title .bi { color: var(--accent); }
.tour-steps { padding: var(--sp-2); display: flex; flex-direction: column; gap: 2px; }
.tour-step {
  display: flex;
  align-items: flex-start;
  gap: var(--sp-3);
  padding: var(--sp-2) var(--sp-3);
  border-radius: var(--r-md);
  color: var(--text);
  text-decoration: none;
}
/* Rows are anchors, so the global a:hover (base.css) would paint them link-blue +
   underlined on hover. Re-assert the row's own look — highlight the background only. */
.tour-step:hover { background: var(--surface-hover); color: var(--text); text-decoration: none; }
.tour-step .bi { margin-top: 2px; color: var(--text-muted); }
.tour-step.done .bi { color: var(--success); }
.tour-step.done .tour-step-label { text-decoration: line-through; color: var(--text-muted); }
.tour-step-text { display: flex; flex-direction: column; gap: 1px; }
.tour-step-label { font-size: 0.9rem; font-weight: 500; }
.tour-step-hint { font-size: 0.78rem; color: var(--text-muted); }
.tour-foot {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: var(--sp-2) var(--sp-4) var(--sp-3);
}
.tour-progress { font-size: 0.78rem; color: var(--text-muted); }
.tour-done-note { padding: var(--sp-3) var(--sp-4) var(--sp-4); font-size: 0.9rem; color: var(--text-muted); }
