/* Theme variables. Dark is the default; [data-theme="light"] overrides. */
:root,
[data-theme="dark"] {
  --bg: #121212;
  --panel: #1e1e1e;
  --row: #242424;
  --row-highlight: #3a3a00;
  --counter-fg: #ffffff;
  --content-fg: #cccccc;
  --timestamp-fg: #888888;
  --match-fg: #7FC1DF;
  --accent: #4caf50;
  --error: #ff6b6b;
  --overlay-dim: #00000088;
  --link: #7FC1DF;
}

[data-theme="light"] {
  --bg: #fafafa;
  --panel: #ffffff;
  --row: #f0f0f0;
  --row-highlight: #fff7b0;
  --counter-fg: #000000;
  --content-fg: #333333;
  --timestamp-fg: #777777;
  --match-fg: #496E80;
  --accent: #2e7d32;
  --error: #c62828;
  --overlay-dim: #cccccc80;
  --link: #496E80;
}

* {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  height: 100%;
}

body {
  background: var(--bg);
  color: var(--content-fg);
  font-family: system-ui, sans-serif;
  overflow: hidden;
}

#app {
  display: flex;
  flex-direction: column;
  height: 100vh;
}

/* --- Camera panel --- */
#camera-panel {
  position: relative;
  flex: 0 0 var(--cam-height, 45%);
  background: #000;
  overflow: hidden;
}

#video,
#freeze,
#overlay {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  object-fit: cover;
}

/* Discard animation: the freeze layers shrink slightly towards the detected
   code (transform-origin set inline by scanner.js), slide down until the code
   center reaches the panel's bottom edge (--discard-shift, set inline at draw
   time), and fade out. The frame itself additionally darkens to 50%
   brightness. Durations come from --discard-ms (set inline from settings on
   discard). Fade and darken are linear; the transform (slide + shrink) uses a
   mild ease-in so the movement accelerates slightly. translateY comes before
   scale so the shift is not scaled down by the shrink. The transition lives
   on .discarding only, so removing the class mid-flight snaps the layers
   back instantly — used when a new freeze interrupts a running discard. */
#freeze.discarding,
#overlay.discarding {
  opacity: 0;
  transform: translateY(var(--discard-shift, 0px)) scale(0.85);
  transition:
    opacity var(--discard-ms, 800ms) linear,
    transform var(--discard-ms, 800ms) cubic-bezier(0.4, 0, 0.8, 0.6);
}

/* Only the frozen frame darkens; the highlight polygon keeps its color. */
#freeze.discarding {
  filter: brightness(0.5);
  transition:
    opacity var(--discard-ms, 800ms) linear,
    filter var(--discard-ms, 800ms) linear,
    transform var(--discard-ms, 800ms) cubic-bezier(0.4, 0, 0.8, 0.6);
}

#scan-content {
  position: absolute;
  left: 0;
  right: 0;
  bottom: 0;
  padding: 8px 12px;
  background: #000000aa;
  color: #fff;
  font-weight: bold;
  word-break: break-all;
}

#camera-error {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  text-align: center;
  padding: 16px;
  color: var(--error);
}

/* Camera off: dark fill with a centered white icon. */
#camera-off {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: #333333;
  color: #ffffff;
  font-size: 64px;
}

/* Placement reticle: four accent corner brackets in the center. */
#reticle {
  position: absolute;
  top: 50%;
  left: 50%;
  width: 60%;
  aspect-ratio: 1 / 1;
  transform: translate(-50%, -50%);
  pointer-events: none;
}

#reticle i {
  position: absolute;
  width: 22px;
  height: 22px;
  border: 3px solid var(--accent);
}

#reticle i:nth-child(1) { top: 0; left: 0; border-right: none; border-bottom: none; }
#reticle i:nth-child(2) { top: 0; right: 0; border-left: none; border-bottom: none; }
#reticle i:nth-child(3) { bottom: 0; left: 0; border-right: none; border-top: none; }
#reticle i:nth-child(4) { bottom: 0; right: 0; border-left: none; border-top: none; }

/* Tap-to-continue hint shown while frozen in tap mode. */
#tap-hint {
  position: absolute;
  left: 0;
  right: 0;
  bottom: 44px;
  text-align: center;
  color: #fff;
  font-weight: bold;
  pointer-events: none;
  text-shadow:
    -1px -1px 0 #000,
    1px -1px 0 #000,
    -1px 1px 0 #000,
    1px 1px 0 #000;
}

.cam-ctrl {
  position: absolute;
  top: 8px;
  width: 40px;
  height: 40px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 22px;
  line-height: 1;
  background: #000000aa;
  color: #fff;
  border: none;
  border-radius: 6px;
  padding: 0;
}

#menu-btn {
  right: 8px;
}

#camera-btn {
  right: 56px;
}

/* --- History --- */
#history {
  flex: 1 1 auto;
  overflow-y: auto;
  background: var(--panel);
}

.entry {
  display: flex;
  align-items: stretch;
  gap: 10px;
  padding: 8px 10px;
  border-bottom: 1px solid #00000022;
  background: var(--row);
}

.entry.highlighted {
  background: var(--row-highlight);
}

/* Left column: fixed width, sized for two-digit counters. */
.entry .col-left {
  flex: 0 0 2.2em;
  display: flex;
  align-items: center;
  justify-content: flex-end;
}

/* Center column: takes the remaining space; content over timestamp. */
.entry .col-center {
  flex: 1 1 auto;
  min-width: 0;
  display: flex;
  flex-direction: column;
  justify-content: center;
}

/* Right column: fixed width, trash centered both ways. */
.entry .col-right {
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  justify-content: center;
}

.entry .counter {
  text-align: right;
  font-weight: bold;
  font-size: 1.4em;
  color: var(--counter-fg);
}

.entry .content {
  font-weight: normal;
  color: var(--content-fg);
  word-break: break-all;
}

.entry .content strong {
  font-weight: bold;
  color: var(--match-fg);
}

.entry .timestamp {
  font-size: 0.75em;
  color: var(--timestamp-fg);
}

.entry .trash {
  background: none;
  border: none;
  color: var(--content-fg);
  font-size: 20px;
  padding: 0 8px;
}

/* Note shown at the end of the entry list while the catalog holds no entries. */
.catalog-hint {
  margin: 12px 10px;
  padding: 10px 12px;
  border: 1px solid var(--timestamp-fg);
  border-radius: 8px;
  background: var(--row);
  color: var(--content-fg);
  font-size: 0.9em;
  line-height: 1.4;
}

/* Inline button styled as a link, used inside .catalog-hint. */
.link-btn {
  background: none;
  border: none;
  padding: 0;
  font: inherit;
  color: var(--link);
  text-decoration: underline;
}

/* --- Bottom bar --- */
#bottom-bar {
  flex: 0 0 auto;
}

#bottom-bar button {
  width: 100%;
  padding: 12px;
  background: var(--accent);
  color: #fff;
  border: none;
  font-weight: bold;
}

/* --- Options overlay --- */
.overlay {
  position: fixed;
  inset: 0;
  background: var(--overlay-dim);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 10;
}

.options-panel {
  position: relative;
  width: 88%;
  max-width: 420px;
  max-height: 80vh;
  overflow-y: auto;
  background: var(--panel);
  color: var(--content-fg);
  border-radius: 10px;
  padding: 0 16px 16px;
}

/* Sticky header: title + close button stay pinned while the body scrolls. */
.options-header {
  position: sticky;
  top: 0;
  z-index: 1;
  display: flex;
  align-items: center;
  justify-content: space-between;
  background: var(--panel);
  padding-top: 4px;
  padding-bottom: 8px;
}

.options-panel h2 {
  margin-top: 0;
}

.options-panel a:link,
.options-panel a:visited,
.options-panel a:active {
  color: var(--link);
}

#options-close,
#preview-close,
#manage-db-close,
#load-url-close {
  font-size: 22px;
  background: none;
  border: none;
  color: var(--content-fg);
  padding: 0;
}

.opt-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin: 12px 0;
}

.opt-group {
  border-top: 1px solid #80808044;
  margin-top: 12px;
  padding-top: 8px;
  /* Keep the heading clear of the sticky options header when scrolled to. */
  scroll-margin-top: 44px;
}

.opt-freeze {
  border: none;
  margin: 8px 0 0;
  padding: 0;
}

.opt-freeze legend {
  padding: 0;
  margin-bottom: 4px;
  font-weight: bold;
}

.opt-freeze-row {
  display: grid;
  grid-template-columns: auto 1fr auto;
  align-items: center;
  gap: 8px;
  margin: 10px 0;
}

.opt-freeze-row input[type="range"] {
  width: 96px;
}

.opt-radios {
  border: none;
  margin: 8px 0 0;
  padding: 0;
}

.opt-radios legend {
  padding: 0;
  margin-bottom: 4px;
  font-weight: bold;
}

.opt-radio-row {
  display: flex;
  align-items: center;
  gap: 8px;
  margin: 8px 0;
}

/* Checkbox row with the control on the left, matching the radio-row layout. */
.opt-check-row {
  display: flex;
  align-items: center;
  gap: 8px;
  margin: 12px 0;
}

/* Accent-colored suffix of the first token in unmatched history entries. */
.tok-accent {
  color: var(--accent);
}

/* --- Catalog import file list --- */
.catalog-file {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 6px 0;
}

.catalog-file-label {
  flex: 1;
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 0;
}

.catalog-file-label input[type="checkbox"] {
  width: 18px;
  height: 18px;
}

/* --- Toast notifications --- */
#toast {
  position: fixed;
  left: 50%;
  bottom: 24px;
  transform: translateX(-50%);
  max-width: 88%;
  padding: 10px 16px;
  border-radius: 8px;
  background: var(--panel);
  color: var(--content-fg);
  border-left: 4px solid var(--accent);
  box-shadow: 0 2px 12px #00000066;
  z-index: 30;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.25s;
}

#toast.toast-visible {
  opacity: 1;
}

#toast.toast-error {
  border-left-color: #e5484d;
}

/* --- Catalog preview overlay --- */
#preview {
  z-index: 20;
}

#preview-error {
  color: #e5484d;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}

#preview-content {
  white-space: pre-wrap;
  overflow-wrap: anywhere;
  font-size: 12px;
  margin: 8px 0 0;
}

.catalog-preview-btn {
  padding: 6px 10px;
  font-size: 13px;
}

#load-url-input {
  width: 100%;
  box-sizing: border-box;
  padding: 8px;
  margin: 10px 0 4px;
}

/* --- Load-from-URL overlay --- */
#load-url {
  z-index: 15;
}

.load-url-actions {
  display: flex;
  gap: 8px;
  margin-top: 8px;
}

/* --- Manage database overlay --- */
#manage-db {
  z-index: 20;
}

.manage-row {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 0;
}

.manage-row input[type="checkbox"] {
  width: 18px;
  height: 18px;
  flex: 0 0 auto;
}

.manage-text {
  flex: 1;
  overflow-wrap: anywhere;
}

.manage-time {
  font-size: 12px;
  color: var(--timestamp-fg);
  white-space: nowrap;
}

.manage-actions {
  display: flex;
  gap: 8px;
  margin-top: 8px;
}

.db-actions {
  display: flex;
  flex-direction: column;
  gap: 8px;
  margin-top: 8px;
}

.manage-sep {
  border: none;
  border-top: 1px solid #80808044;
  margin: 16px 0 12px;
}

.manual-row {
  display: flex;
  gap: 8px;
  margin-bottom: 8px;
}

.manual-row input[type="text"] {
  flex: 1;
  min-width: 0;
}

#manual-entries-limit {
  font-size: 12px;
  color: var(--timestamp-fg);
}

[hidden] {
  display: none !important;
}
