:root {
  --bg-top: #0d0d1a;
  --bg-bottom: #1a0d26;
  --card: rgba(255,255,255,0.08);
  --card-soft: rgba(255,255,255,0.05);
  --border: rgba(255,255,255,0.12);
  --text: #f1f0f5;
  --text-dim: #9a99a6;
  --accent: #2f6bf0;
  --accent-strong: #1c5fd9;
  --gold: #f0b93e;
  --danger: #ff6b6b;
  --success: #4cd97b;
  color-scheme: dark;
}

* { box-sizing: border-box; }

/* Author-stylesheet `display` rules elsewhere in this file (e.g. `.preview-wrap
   { display: flex }`) would otherwise beat the UA stylesheet's `[hidden]
   { display: none }`, since author styles win over UA styles regardless of
   specificity — this guarantees el.hidden = true always actually hides it. */
[hidden] { display: none !important; }

html, body {
  margin: 0; padding: 0; height: 100%;
  background: linear-gradient(135deg, var(--bg-top), var(--bg-bottom));
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  -webkit-font-smoothing: antialiased;
  overscroll-behavior-y: none;
}

button { font-family: inherit; }

.topbar {
  display: flex; align-items: flex-start; gap: 12px;
  padding: max(20px, env(safe-area-inset-top)) 20px 8px;
}
.topbar-text { flex: 1; min-width: 0; }
.topbar h1 { font-size: 26px; font-weight: 700; margin: 0 0 4px; }
.subtitle { margin: 0; font-size: 14px; color: var(--text-dim); }

.icon-btn {
  background: none; border: none; color: var(--text-dim);
  width: 34px; height: 34px; padding: 0; cursor: pointer;
  display: flex; align-items: center; justify-content: center;
}
.icon-btn svg { width: 22px; height: 22px; fill: currentColor; }
.icon-btn:hover { color: var(--text); }

.text-btn {
  background: none; border: none; color: var(--accent);
  font-size: 16px; font-weight: 600; cursor: pointer; padding: 6px 4px;
}
.text-btn.destructive { color: var(--danger); }

.link-btn {
  background: none; border: none; color: var(--text-dim);
  font-size: 14px; font-weight: 600; cursor: pointer; padding: 10px 0 0;
}

.content { max-width: 640px; margin: 0 auto; padding: 12px 20px 60px; }

.disclosure { font-size: 12px; color: var(--text-dim); margin: 0 0 16px; }

.drop-zone {
  border: 1.5px dashed var(--border); border-radius: 16px;
  padding: 48px 20px; text-align: center; cursor: pointer;
  background: var(--card-soft); transition: border-color .15s, background .15s;
}
.drop-zone.dragover { border-color: var(--accent); background: rgba(47,107,240,0.1); }
.drop-icon { width: 44px; height: 44px; fill: var(--accent); margin-bottom: 12px; }
.drop-title { font-size: 17px; font-weight: 600; margin: 0 0 4px; }
.drop-sub { font-size: 13px; color: var(--text-dim); margin: 0; }

.preview-wrap { display: flex; flex-direction: column; align-items: stretch; }
.preview-wrap img#previewImg {
  max-height: 340px; width: 100%; object-fit: contain;
  border: 2px solid var(--accent); border-radius: 12px; background: #000;
}

.working { display: flex; justify-content: center; padding: 20px; }
.spinner {
  width: 28px; height: 28px; border-radius: 50%;
  border: 3px solid rgba(255,255,255,0.2); border-top-color: var(--accent);
  animation: spin .8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

.result-card {
  background: var(--card); border-radius: 12px; padding: 16px; margin-top: 14px;
}
.result-card h3 { font-size: 18px; font-weight: 700; margin: 0 0 10px; }
.result-summary { font-size: 14px; color: var(--text-dim); margin: 0 0 14px; line-height: 1.45; }
.message-card {
  background: var(--card-soft); border-radius: 12px; padding: 16px; margin-top: 14px;
  font-size: 14px;
}

.diagram-wrap {
  background: rgba(255,255,255,0.04); border-radius: 14px; padding: 10px;
  margin-bottom: 10px;
}
.diagram-legend { font-size: 11px; color: var(--text-dim); margin: 8px 0 14px; }

.light-row { display: flex; gap: 10px; padding: 10px 0; border-top: 1px solid var(--border); }
.light-row:first-child { border-top: none; }
.light-dot { width: 12px; height: 12px; border-radius: 50%; margin-top: 4px; flex: none; }
.light-info h4 { font-size: 15px; font-weight: 600; margin: 0 0 3px; }
.light-info p { font-size: 13px; color: var(--text-dim); margin: 2px 0; line-height: 1.4; }
.light-info .gear { font-weight: 600; }
.light-info .note { font-style: italic; opacity: .85; }

.tips-block, .save-block { background: var(--card-soft); border-radius: 8px; padding: 12px; margin-top: 12px; }
.tips-block h4, .save-block h4 { font-size: 14px; font-weight: 600; margin: 0 0 6px; }
.tips-block ul { margin: 0; padding-left: 18px; font-size: 13px; color: var(--text-dim); }
.tips-block li { margin-bottom: 4px; }

.pill-btn {
  background: rgba(47,107,240,0.85); color: #fff; border: none; border-radius: 8px;
  padding: 8px 14px; font-size: 13px; font-weight: 600; cursor: pointer;
}
.pill-btn.secondary { background: rgba(255,255,255,0.08); color: var(--text-dim); }
.pill-btn:disabled { opacity: .5; cursor: default; }
.save-row { display: flex; gap: 10px; flex-wrap: wrap; margin-top: 6px; }
.saved-tag { font-size: 13px; color: var(--success); display: flex; align-items: center; gap: 6px; }

/* Sheets (dialogs) — the [open] qualifier matters: without it these rules'
   `display: flex` would override the browser's default "hidden unless
   open" behavior for <dialog>, making every sheet render inline on the
   page instead of as a modal. */
dialog.sheet:not([open]) { display: none; }
dialog.sheet[open] {
  border: none; border-radius: 16px 16px 0 0; padding: 0;
  width: 100%; max-width: 640px; height: 88vh; max-height: 88vh;
  margin: auto auto 0; background: linear-gradient(135deg, var(--bg-top), var(--bg-bottom));
  color: var(--text); overflow: hidden; display: flex; flex-direction: column;
}
dialog.sheet.small[open] { height: auto; max-height: 85vh; border-radius: 16px; margin: auto; }
dialog.sheet::backdrop { background: rgba(0,0,0,0.55); }

.sheet-header {
  display: flex; align-items: center; justify-content: space-between;
  padding: max(16px, env(safe-area-inset-top)) 16px 12px; border-bottom: 1px solid var(--border);
  flex: none;
}
.sheet-header h2 { font-size: 17px; font-weight: 700; margin: 0; }
.sheet-actions { display: flex; align-items: center; gap: 8px; }

.menu-wrap { position: relative; }
.menu {
  display: none; position: absolute; right: 0; top: 40px; z-index: 20;
  background: #1c1b28; border: 1px solid var(--border); border-radius: 10px;
  min-width: 200px; padding: 6px; box-shadow: 0 8px 24px rgba(0,0,0,0.4);
}
.menu.open { display: block; }
.menu button {
  display: block; width: 100%; text-align: left; background: none; border: none;
  color: var(--text); font-size: 14px; padding: 10px 10px; border-radius: 6px; cursor: pointer;
}
.menu button:hover { background: rgba(255,255,255,0.08); }

.search-field {
  background: rgba(255,255,255,0.08); border: 1px solid var(--border); border-radius: 10px;
  color: var(--text); font-size: 15px; padding: 10px 12px; margin: 12px 16px 0; outline: none;
}
.search-field:focus { border-color: var(--accent); }

.library-grid {
  display: grid; grid-template-columns: repeat(auto-fill, minmax(96px, 1fr)); gap: 8px;
  padding: 14px 16px; overflow-y: auto; flex: 1;
  /* Without this, a grid container taller than its content (the normal
     case with few items, since the dialog itself is a fixed 88vh) stretches
     its auto-sized row tracks apart to fill the leftover space — rows end
     up pulled apart with large gaps between them instead of packed at the
     top. align-content: start keeps rows at their natural height and packs
     them from the top, leaving any leftover space below the last row. */
  align-content: start;
}
.library-thumb {
  aspect-ratio: 1; border-radius: 8px; overflow: hidden; border: none; padding: 0;
  cursor: pointer; background: rgba(255,255,255,0.08);
}
.library-thumb img { width: 100%; height: 100%; object-fit: cover; display: block; }

.empty-note { text-align: center; color: var(--text-dim); font-size: 14px; margin-top: 60px; }

.restoring-overlay {
  position: absolute; inset: 0; background: rgba(0,0,0,0.5);
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 12px;
  color: #fff; font-size: 14px;
}

.tags-preview { max-height: 280px; width: calc(100% - 32px); object-fit: contain; border-radius: 12px; margin: 16px; background: #000; }
.field-label { font-size: 13px; color: var(--text-dim); margin: 16px 16px 6px; display: block; }
.footnote { font-size: 12px; color: var(--text-dim); margin: 8px 16px; line-height: 1.4; }

#itemDialogBody { padding: 0 16px 16px; overflow-y: auto; }
#itemDialogBody img { width: 100%; max-height: 300px; object-fit: contain; border-radius: 12px; background: #000; margin-bottom: 12px; }

.toast {
  position: fixed; bottom: max(24px, env(safe-area-inset-bottom)); left: 50%; transform: translateX(-50%);
  background: #222; color: #fff; padding: 10px 16px; border-radius: 10px; font-size: 13px;
  z-index: 100; max-width: 90vw; text-align: center;
}
