:root {
  --bg: #0c0e12;
  --surface: #161a21;
  --surface-2: #1d2230;
  --text: #e7e9ec;
  --muted: #8c93a0;
  --accent: #5b8def;
  --accent-strong: #3a6dd9;
  --danger: #ef5b5b;
  --success: #4ade80;
  --border: #262c38;
  --radius: 8px;
}

* { box-sizing: border-box; }
html, body {
  margin: 0; padding: 0;
  background: var(--bg); color: var(--text);
  font: 14px/1.5 system-ui, -apple-system, "Segoe UI", Inter, sans-serif;
  min-height: 100vh;
}

a { color: var(--accent); text-decoration: none; }
a:hover { color: var(--accent-strong); }

button, input, select, textarea {
  font: inherit; color: inherit;
}

/* Form controls — explicit dark-theme styling with !important to beat
 * native macOS / Windows form rendering, which otherwise paints a
 * faint gradient overlay on selects + inputs that washes out the
 * background. Without these overrides the page surface bleeds through
 * and the control looks "mờ mịt". --field-bg is one tier brighter
 * than --surface-2 so the control reads as a clearly distinct surface. */
:root {
  --field-bg: #2a3142;
  --field-bg-focus: #323a4f;
}

input:not([type="checkbox"]):not([type="radio"]):not([type="file"]):not([type="range"]),
select,
textarea {
  background-color: var(--field-bg) !important;
  color: var(--text) !important;
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 7px 10px;
  outline: none;
  transition: border-color .12s, background-color .12s;
}
input:not([type="checkbox"]):not([type="radio"]):not([type="file"]):not([type="range"]):hover,
select:hover,
textarea:hover {
  border-color: var(--muted);
}
input:not([type="checkbox"]):not([type="radio"]):not([type="file"]):not([type="range"]):focus,
select:focus,
textarea:focus {
  border-color: var(--accent) !important;
  background-color: var(--field-bg-focus) !important;
}
input::placeholder,
textarea::placeholder {
  color: var(--muted) !important;
  opacity: 1;
}

/* Native <select> on macOS/iOS still paints a system overlay even with
 * `appearance:none` unless we also zero out the box-shadow inset that
 * some UAs use to draw the bevel. Explicit `color-scheme: dark` tells
 * the OS we want dark-mode form rendering — kills the white-overlay
 * fallback some browsers fall back to. */
html { color-scheme: dark; }
select {
  -webkit-appearance: none !important;
  -moz-appearance: none !important;
  appearance: none !important;
  padding-right: 30px !important;
  background-image: url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 24 24' fill='none' stroke='%23e7e9ec' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'><polyline points='6 9 12 15 18 9'/></svg>") !important;
  background-repeat: no-repeat !important;
  background-position: right 10px center !important;
  background-size: 12px 12px !important;
  box-shadow: none !important;
  text-shadow: none !important;
}
/* Each <option> in the open dropdown gets the system widget's colours
 * in most browsers; Chromium + Firefox honour an explicit color here. */
select option {
  background-color: var(--field-bg) !important;
  color: var(--text) !important;
}
select option:checked,
select option:hover {
  background-color: var(--accent) !important;
  color: #fff !important;
}

.topbar {
  display: flex; justify-content: space-between; align-items: center;
  padding: 12px 24px;
  border-bottom: 1px solid var(--border);
  background: var(--surface);
  position: sticky; top: 0; z-index: 10;
}
.brand {
  font-weight: 700;
  font-size: 16px;
  color: var(--text);
  display: inline-flex;
  align-items: center;
  gap: 8px;
  text-decoration: none;
}
.brand-mark { display: block; flex: 0 0 auto; }
.userbar { display: flex; gap: 12px; align-items: center; font-size: 13px; color: var(--muted); }
.userbar .signedin { color: var(--text); }
.userbar button { background: none; border: none; color: var(--muted); cursor: pointer; }
.userbar button:hover { color: var(--text); }

main { max-width: 980px; margin: 0 auto; padding: 32px 24px; }

h1 { font-size: 22px; margin: 0 0 16px; letter-spacing: -0.01em; }
h2 { font-size: 16px; margin: 24px 0 12px; color: var(--muted); text-transform: uppercase; letter-spacing: 0.08em; }

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 20px;
  margin-bottom: 16px;
}

form.stack { display: flex; flex-direction: column; gap: 12px; }
form.stack label { display: flex; flex-direction: column; gap: 4px; font-size: 13px; color: var(--muted); }
form.stack input, form.stack textarea, form.stack select {
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 8px 12px;
  color: var(--text);
}
form.stack input:focus, form.stack textarea:focus {
  outline: none; border-color: var(--accent);
}

.row { display: flex; gap: 12px; flex-wrap: wrap; align-items: center; }

.btn {
  display: inline-flex; align-items: center; gap: 6px;
  padding: 8px 14px;
  border-radius: var(--radius);
  border: 1px solid transparent;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.15s, border-color 0.15s, color 0.15s;
  white-space: nowrap;
}
.btn.primary { background: var(--accent); color: #fff; }
.btn.primary:hover { background: var(--accent-strong); }
.btn.ghost { background: transparent; color: var(--text); border-color: var(--border); }
.btn.ghost:hover { border-color: var(--accent); color: var(--accent); }
.btn.danger { background: transparent; color: var(--danger); border-color: rgba(239,91,91,0.3); }
.btn.danger:hover { background: rgba(239,91,91,0.1); }
.btn.sm { padding: 4px 10px; font-size: 12px; }

.tabs { display: flex; gap: 4px; border-bottom: 1px solid var(--border); margin: 0 -20px 20px; padding: 0 20px; }
.tabs button {
  background: transparent; border: none;
  padding: 10px 14px; color: var(--muted); cursor: pointer; border-bottom: 2px solid transparent;
}
.tabs button.active { color: var(--text); border-color: var(--accent); }
.tabs button:hover { color: var(--text); }

/* Two-level nav: group row (top, bold) + subtabs row (lighter). Phase 1. */
.tabs-group button { font-weight: 600; font-size: 14px; }
.subtabs { display: flex; gap: 4px; margin: 0 -20px 18px; padding: 2px 20px 0; flex-wrap: wrap; }
.subtabs button { background: transparent; border: none; padding: 6px 12px; color: var(--muted); cursor: pointer; border-radius: 6px; font-size: 13px; }
.subtabs button.active { color: var(--text); background: var(--surface-2); }
.subtabs button:hover { color: var(--text); }

/* Principal picker — shared people/role/group autocomplete. Phase 0. */
.pp-wrap { position: relative; }
.pp-drop { position: absolute; z-index: 40; left: 0; right: 0; margin-top: 2px; background: var(--surface-2); border: 1px solid var(--border); border-radius: var(--radius); max-height: 260px; overflow-y: auto; box-shadow: 0 8px 24px rgba(0,0,0,.35); }
.pp-item { display: flex; align-items: center; gap: 8px; padding: 8px 10px; cursor: pointer; font-size: 13px; }
.pp-item[data-i]:hover { background: var(--field-bg-focus); }
.pp-ico { width: 18px; text-align: center; }
.pp-label { color: var(--text); }
.pp-sub { margin-left: auto; }
.pp-raw { margin-top: 4px; }

/* ACL chips (rendered by consumers via principalChip). Phase 0/2/3. */
.acl-chip { display: inline-flex; align-items: center; gap: 5px; padding: 3px 6px 3px 8px; margin: 2px; border-radius: 14px; background: var(--surface-2); border: 1px solid var(--border); font-size: 12px; }
.acl-chip.unknown { border-style: dashed; color: var(--muted); }
.acl-chip .chip-x { cursor: pointer; color: var(--muted); border: none; background: transparent; font-size: 13px; line-height: 1; padding: 0 2px; }
.acl-chip .chip-x:hover { color: var(--danger); }
.acl-chip .chip-sub { color: var(--muted); font-size: 11px; }

/* Inline ABI reference blocks (WASM panel). */
.abi-pre { font-family: ui-monospace, "SF Mono", Menlo, Consolas, monospace; font-size: 11px; background: var(--surface-2); color: var(--text); padding: 8px 10px; border-radius: 6px; overflow-x: auto; white-space: pre-wrap; margin: 6px 0; }

.empty { color: var(--muted); font-size: 13px; padding: 24px; text-align: center; border: 1px dashed var(--border); border-radius: var(--radius); }

table {
  width: 100%; border-collapse: collapse; font-size: 13px;
}
table th, table td {
  padding: 10px 12px; text-align: left;
  border-bottom: 1px solid var(--border);
}
table th { color: var(--muted); font-weight: 600; font-size: 11px; text-transform: uppercase; letter-spacing: 0.06em; }
table tr:last-child td { border-bottom: none; }
.mono { font-family: ui-monospace, "SF Mono", Menlo, monospace; font-size: 12px; }
.muted { color: var(--muted); }

.tag {
  display: inline-block; padding: 2px 8px;
  border-radius: 999px; font-size: 11px;
  background: var(--surface-2); color: var(--muted);
  border: 1px solid var(--border);
}
.tag.ok { color: var(--success); border-color: rgba(74,222,128,0.3); }
.tag.warn { color: #fbbf24; border-color: rgba(251,191,36,0.3); }

.userbar-name {
  font-weight: 500;
  color: var(--text);
  text-decoration: none;
}
.userbar-name:hover { color: var(--accent); }

.field-error {
  display: block;
  min-height: 16px;
  margin-top: 4px;
  color: var(--danger);
  font-size: 12px;
}
.field-hint {
  display: block;
  margin-top: 4px;
  font-size: 12px;
}
input.invalid {
  border-color: var(--danger) !important;
}
input.invalid:focus {
  outline-color: var(--danger);
}

.divider {
  display: flex;
  align-items: center;
  gap: 8px;
  margin: 16px 0;
  color: var(--muted);
  font-size: 12px;
}
.divider::before, .divider::after {
  content: "";
  flex: 1;
  height: 1px;
  background: var(--border);
}

.token-copy {
  appearance: none;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 6px;
  color: var(--text);
  font: inherit;
  font-size: 12px;
  padding: 3px 8px;
  cursor: pointer;
  transition: background 0.12s, border-color 0.12s;
}
.token-copy:hover {
  background: rgba(91,141,239,0.12);
  border-color: var(--accent);
}
.token-copy:active {
  background: rgba(91,141,239,0.22);
}

/* Stage tabs sit at the very top edge of the .card they're inside, sharing
   the look of the app-level .tabs but bound to a single card. The negative
   margin pulls the row up to cancel the card's 20px top padding. */
.stage-tabs {
  display: flex;
  align-items: center;
  gap: 4px;
  border-bottom: 1px solid var(--border);
  margin: -20px -20px 16px;
  padding: 0 12px;
}
.stage-tabs > button[data-stage] {
  appearance: none;
  background: transparent;
  border: 0;
  border-bottom: 2px solid transparent;
  padding: 12px 16px;
  color: var(--muted);
  font: inherit;
  font-size: 13px;
  font-weight: 500;
  cursor: pointer;
  transition: color 0.12s, border-color 0.12s;
  /* nudge button border-bottom flush against the row border-bottom */
  margin-bottom: -1px;
}
.stage-tabs > button[data-stage]:hover { color: var(--text); }
.stage-tabs > button[data-stage].active {
  color: var(--text);
  border-color: var(--accent);
  cursor: default;
}
.stage-tabs-actions {
  margin-left: auto;
  gap: 8px;
  padding: 6px 0;
}
.stage-tab-label {
  display: inline-flex;
  align-items: center;
  gap: 6px;
}
.stage-pill {
  font-size: 11px;
  font-weight: 500;
  color: var(--muted);
  background: var(--surface, #f1f5f9);
  padding: 2px 8px;
  border-radius: 999px;
  letter-spacing: 0.02em;
}
.stage-pill:empty { display: none; }
.stage-pill-zero { opacity: 0.6; font-style: italic; }

/* Inline help banner above the file list — explains the current
   stage so first-time users don't need to discover the tab tooltips.
   Tone-keyed background so a drift warning stands out vs the steady
   info state. */
.stage-help {
  font-size: 13px;
  line-height: 1.5;
  padding: 10px 14px;
  border-radius: 8px;
  margin-bottom: 14px;
  background: #f8fafc;
  border: 1px solid var(--border);
  color: var(--text);
}
.stage-help[data-tone="warn"] {
  background: #fff8e6;
  border-color: #f4d27a;
}
.stage-help[data-tone="live"] {
  background: #ecfdf5;
  border-color: #b1e5cf;
}
.stage-help strong { font-weight: 600; }
/* xs button for inline use inside help banners — same primary/ghost
   palette as sm, just smaller padding so it doesn't dominate the line. */
.btn.xs {
  padding: 4px 10px;
  font-size: 12px;
  border-radius: 6px;
  line-height: 1.4;
}

/* First-time guide — only visible when the app has no files in either
   stage. Sits between the stage-tabs and the Upload heading so it
   explains the workflow before the user wastes a click. */
.first-time-guide {
  background: linear-gradient(135deg, #f0f7ff 0%, #f8fafc 100%);
  border: 1px solid #c7e0ff;
  border-radius: 10px;
  padding: 16px 18px;
  margin-bottom: 16px;
}
.first-time-guide .ftg-head {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 14px;
  margin-bottom: 10px;
  color: var(--text);
}
.first-time-guide .ftg-ico { font-size: 18px; }
.first-time-guide .ftg-steps {
  margin: 0;
  padding-left: 22px;
  font-size: 13px;
  line-height: 1.6;
  color: var(--text);
}
.first-time-guide .ftg-steps li { margin-bottom: 4px; }
.first-time-guide .ftg-foot { margin: 10px 0 0; }

/* Upload panel heading row + a prominent destination chip so the operator
   always sees WHERE an upload will land (tracks state.currentDir). Sits on the
   same line as the "UPLOAD" title. */
.upload-head {
  display: flex;
  align-items: center;
  gap: 12px;
  flex-wrap: wrap;
  margin: 0 0 12px;
}
.upload-head h2 { margin: 0; }
.dz-dest {
  display: inline-flex;
  align-items: baseline;
  gap: 6px;
  padding: 3px 12px;
  font-size: 13px;
  line-height: 1.6;
  color: var(--muted);
  background: rgba(91, 141, 239, 0.12);
  border: 1px solid rgba(91, 141, 239, 0.45);
  border-radius: 999px;
  white-space: nowrap;
}
.dz-dest strong {
  color: var(--accent);
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-weight: 600;
}
/* At root there's no folder to emphasise — keep it calm. */
.dz-dest.is-root strong { color: var(--muted); }

.dropzone {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 10px;
  border: 2px dashed var(--border);
  border-radius: var(--radius);
  padding: 32px;
  text-align: center;
  color: var(--muted);
  transition: border-color 0.15s, background 0.15s, box-shadow 0.15s;
  cursor: pointer;
}
.dropzone:hover, .dropzone.dragover {
  border-color: var(--accent);
  /* Light the WHOLE area up on hover (not just the buttons) — clearly visible
     so it's obvious the entire zone is a drop/click target. */
  background: rgba(91,141,239,0.12);
  color: var(--text);
  box-shadow: inset 0 0 0 1px var(--accent), 0 0 0 1px rgba(91,141,239,0.35);
}
/* The buttons' own hover shouldn't fight the zone highlight — keep the zone lit. */
.dropzone .dz-actions .btn:hover {
  border-color: var(--accent);
}
.dropzone .dz-title { font-size: 14px; }
.dropzone .dz-title strong { color: var(--text); }
.dropzone .dz-hint { font-size: 12px; max-width: 480px; line-height: 1.5; }
/* Action buttons inside the dropzone — `pointer-events: auto` makes sure
   the buttons stay clickable even when the parent <label> claims clicks
   for its file input. */
.dropzone .dz-actions { margin-top: 4px; gap: 8px; justify-content: center; }
.dropzone .dz-actions .btn { pointer-events: auto; cursor: pointer; }
.dropzone input[type=file] { display: none; }
.dropzone.uploading {
  pointer-events: none;
  opacity: 0.6;
}

.upload-status {
  margin-top: 12px;
  display: flex;
  flex-direction: column;
  gap: 8px;
  padding: 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface-2);
}
/* `display: flex` above wins over the UA stylesheet's `[hidden] { display:none }`
   at equal specificity (author > UA), so the panel would stay visible even with
   the attribute set. Restore the hide behaviour with a higher-specificity rule. */
.upload-status[hidden] {
  display: none;
}
.upload-status-row {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  gap: 12px;
  font-size: 12px;
}
.upload-status .small { font-size: 12px; }
.upload-status progress {
  appearance: none;
  -webkit-appearance: none;
  height: 6px;
  border: 0;
  border-radius: 999px;
  background: var(--surface-2);
  overflow: hidden;
}
.upload-status progress::-webkit-progress-bar {
  background: var(--surface-2);
  border-radius: 999px;
}
.upload-status progress::-webkit-progress-value {
  background: var(--accent);
  border-radius: 999px;
  transition: width 0.15s ease;
}
.upload-status progress::-moz-progress-bar {
  background: var(--accent);
  border-radius: 999px;
}

.upload-file-list {
  list-style: none;
  margin: 6px 0 0;
  padding: 0;
  max-height: 220px;
  overflow-y: auto;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface-2);
}
.upload-file-list:empty {
  display: none;
}
.upload-file-item {
  display: grid;
  grid-template-columns: 18px 1fr auto;
  gap: 10px;
  align-items: center;
  padding: 6px 10px;
  border-bottom: 1px solid var(--border);
  font-size: 12px;
}
.upload-file-item:last-child {
  border-bottom: 0;
}
.upload-file-icon {
  text-align: center;
  font-size: 13px;
  line-height: 1;
}
.upload-file-name {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.upload-file-meta {
  font-size: 11px;
  white-space: nowrap;
}
.upload-file-item.state-pending { color: var(--muted); }
.upload-file-item.state-uploading {
  background: rgba(91, 141, 239, 0.06);
}
.upload-file-item.state-uploading .upload-file-icon { color: var(--accent); }
.upload-file-item.state-done .upload-file-icon { color: var(--success); }
.upload-file-item.state-failed { background: rgba(239, 91, 91, 0.08); }
.upload-file-item.state-failed .upload-file-icon,
.upload-file-item.state-failed .upload-file-meta { color: var(--danger); }

/* Icon picker — shared by Create + Edit forms. Square clickable card with
   inline file input (hidden) and a preview that swaps to a background
   image once a file is chosen. */
.app-icon-picker {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
  cursor: pointer;
  user-select: none;
}
.app-icon-preview {
  width: 56px;
  height: 56px;
  border-radius: 12px;
  background: var(--surface-2);
  background-size: cover;
  background-position: center;
  border: 2px dashed var(--border);
  display: grid;
  place-items: center;
  font-size: 24px;
  color: var(--muted);
  transition: border-color 0.15s, color 0.15s;
}
.app-icon-picker:hover .app-icon-preview {
  border-color: var(--accent);
  color: var(--accent);
}
/* Error state — applied transiently when the create form is submitted
   without picking a logo. JS removes the class on next pointer interaction
   or successful file pick so the red flash doesn't linger. */
.app-icon-picker.error .app-icon-preview {
  border-color: var(--danger);
  color: var(--danger);
}
.app-icon-picker.error .app-icon-hint { color: var(--danger); }
.app-icon-hint { font-size: 11px; }

/* Per-row icon in the apps list. */
.app-row-icon {
  display: inline-block;
  width: 32px;
  height: 32px;
  border-radius: 8px;
  background: var(--surface-2);
  background-size: cover;
  background-position: center;
  vertical-align: middle;
}
.app-row-icon--letter {
  display: inline-grid;
  place-items: center;
  font-weight: 600;
  font-size: 14px;
  color: var(--accent);
  background: rgba(91, 141, 239, 0.12);
}

/* Truncate long descriptions / tids so the list stays one row per app. */
.apps-table td.truncate {
  max-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.toast {
  position: fixed;
  bottom: 24px; right: 24px;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 12px 16px;
  max-width: 360px;
  font-size: 13px;
  box-shadow: 0 10px 30px rgba(0,0,0,0.4);
  z-index: 100;
}
.toast.error { border-color: var(--danger); }
.toast.success { border-color: var(--success); }
.toast.warn { border-color: #fbbf24; }

.auth-wrap { max-width: 380px; margin: 64px auto; }
.auth-wrap h1 { text-align: center; }
.auth-switch { text-align: center; margin-top: 12px; font-size: 13px; color: var(--muted); }

.kv { display: grid; grid-template-columns: 120px 1fr; gap: 8px 16px; font-size: 13px; }
.kv dt { color: var(--muted); }
.kv dd { margin: 0; }

.preview-link {
  display: inline-flex; align-items: center; gap: 4px;
  font-size: 12px;
}

.modal-backdrop {
  position: fixed; inset: 0; z-index: 200;
  background: rgba(0,0,0,0.5);
  display: flex; align-items: center; justify-content: center;
  padding: 24px;
}
.modal {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 24px;
  max-width: 560px; width: 100%;
  max-height: 85vh; overflow: auto;
  box-shadow: 0 24px 64px rgba(0,0,0,0.5);
}
.modal h2 { font-size: 16px; color: var(--text); text-transform: none; letter-spacing: 0; margin-bottom: 12px; }
.modal label { font-size: 12px; }

.breadcrumb {
  font-size: 13px;
  display: inline-flex; align-items: center; gap: 6px; flex-wrap: wrap;
}
.breadcrumb a { color: var(--text); }
.breadcrumb a:hover { color: var(--accent); }

a.folder-link {
  font-weight: 500;
  text-decoration: none;
}
a.folder-link:hover { color: var(--accent); text-decoration: underline; }

/* Advanced-mode reveal — hide raw IDs, internal jargon, and developer-only
 * controls by default so business users see a clean surface. Toggling
 * `body.advanced` from the topbar pill reveals everything. Persisted in
 * localStorage so the choice survives page reload. */
.advanced-only { display: none !important; }
body.advanced .advanced-only { display: revert !important; }
body.advanced dl.kv > .advanced-only { display: block !important; }
body.advanced th.advanced-only,
body.advanced td.advanced-only { display: table-cell !important; }
body.advanced span.advanced-only { display: inline !important; }

.advanced-toggle {
  display: inline-flex; align-items: center; gap: 6px;
  font-size: 12px; color: var(--muted);
  cursor: pointer; user-select: none;
  padding: 4px 10px; border: 1px solid var(--border); border-radius: 999px;
  background: transparent; transition: background .15s, color .15s, border-color .15s;
}
.advanced-toggle:hover { color: var(--text); border-color: var(--muted); }
body.advanced .advanced-toggle {
  color: var(--accent); border-color: var(--accent);
  background: rgba(91,141,239,0.08);
}
.advanced-toggle .dot {
  width: 6px; height: 6px; border-radius: 50%;
  background: var(--muted); transition: background .15s;
}
body.advanced .advanced-toggle .dot { background: var(--accent); }
