/* Microfilm Viewer */
/*
 * © 2026 DOSAYGO. All rights reserved.
 * 
 * Reuse and redistribution of this code is strictly prohibited outside of the Microfilm viewer.
 * Using this code to train artificial intelligence models, or reverse engineering it to 
 * build similar products, is not permitted.
 */

:root {
  --bg: #f5f5f5;
  --bg-surface: #ffffff;
  --bg-panel: #fafafa;
  --text: #1a1a1a;
  --text-muted: #666;
  --border: #e5e7eb;
  --accent: #2563eb;
  --accent-hover: #1d4ed8;
  --toolbar-bg: rgba(255,255,255,0.92);
  --thumb-active: #2563eb;
  --shadow: 0 1px 3px rgba(0,0,0,0.08);
  --shadow-lg: 0 4px 12px rgba(0,0,0,0.1);
  --focus-ring: var(--accent);
  --focus-halo: color-mix(in srgb, var(--accent) 22%, transparent);
  --radius-sm: 4px;
  --radius-md: 8px;
  --radius-lg: 12px;
  --space-1: 4px;
  --space-2: 8px;
  --space-3: 12px;
  --space-4: 16px;
  --space-5: 20px;
  --space-6: 24px;
  --line-height-body: 1.5;
  --line-height-tight: 1.2;
  --transition: 150ms ease;
}

html.dark {
  --bg: #111111;
  --bg-surface: #1a1a1a;
  --bg-panel: #151515;
  --text: #e5e5e5;
  --text-muted: #888;
  --border: #2a2a2a;
  --toolbar-bg: rgba(17,17,17,0.92);
  --shadow: 0 1px 3px rgba(0,0,0,0.3);
  --shadow-lg: 0 4px 12px rgba(0,0,0,0.4);
}

*, *::before, *::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

html, body {
  height: 100%;
  overflow: hidden;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  font-size: 14px;
  line-height: var(--line-height-body);
  background: var(--bg);
  color: var(--text);
  -webkit-font-smoothing: antialiased;
  overscroll-behavior: contain;
}

button,
input {
  font: inherit;
}

#app {
  display: flex;
  flex-direction: column;
  height: 100%;
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
}

button,
[role="button"],
#toolbar,
#play-controls,
#thumbnail-panel,
#branding {
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
}

input,
textarea,
select,
dialog,
dialog *,
#microfilm2-hud,
#microfilm2-hud *,
#microfilm2-stream-hud,
#microfilm2-stream-hud * {
  user-select: text;
  -webkit-user-select: text;
  -webkit-touch-callout: default;
}

/* Toolbar */
#toolbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 var(--space-4);
  height: 48px;
  background: var(--toolbar-bg);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  border-bottom: 1px solid var(--border);
  z-index: 100;
  flex-shrink: 0;
}

.toolbar-left, .toolbar-center, .toolbar-right {
  display: flex;
  align-items: center;
  gap: var(--space-2);
}

.toolbar-left { flex: 1; min-width: 0; }
.toolbar-center { flex: 0 0 auto; }
.toolbar-right { flex: 1; justify-content: flex-end; }

.title {
  font-weight: 600;
  font-size: 14px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  letter-spacing: -0.01em;
}

#toolbar button {
  background: none;
  border: 1px solid transparent;
  color: var(--text);
  cursor: pointer;
  min-width: 36px;
  min-height: 36px;
  padding: 0;
  border-radius: var(--radius-md);
  font-size: 16px;
  line-height: 1;
  transition: background var(--transition), border-color var(--transition);
  user-select: none;
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
}

#toolbar button:hover {
  background: var(--bg-surface);
  border-color: var(--border);
}

#toolbar button[aria-pressed="true"] {
  border-color: rgba(0, 168, 214, 0.86);
  background:
    linear-gradient(90deg, rgba(0, 214, 255, 0.16) 0 1px, transparent 1px calc(100% - 1px), rgba(0, 214, 255, 0.16) calc(100% - 1px)),
    linear-gradient(rgba(0, 214, 255, 0.16) 0 1px, transparent 1px calc(100% - 1px), rgba(0, 214, 255, 0.16) calc(100% - 1px)),
    rgba(220, 251, 255, 0.94);
  color: #00384b;
  box-shadow: 0 0 0 1px rgba(0, 44, 56, 0.08), 0 6px 16px rgba(0, 84, 120, 0.12);
}

html.dark #toolbar button[aria-pressed="true"] {
  border-color: rgba(0, 216, 255, 0.88);
  background:
    linear-gradient(90deg, rgba(0, 216, 255, 0.18) 0 1px, transparent 1px calc(100% - 1px), rgba(0, 216, 255, 0.18) calc(100% - 1px)),
    linear-gradient(rgba(0, 216, 255, 0.18) 0 1px, transparent 1px calc(100% - 1px), rgba(0, 216, 255, 0.18) calc(100% - 1px)),
    rgba(0, 16, 24, 0.94);
  color: #7eefff;
}

#toolbar button:active {
  transform: scale(0.96);
}

#toolbar button svg,
#play-controls button svg {
  width: 18px;
  height: 18px;
  display: block;
  margin: auto;
  fill: none;
  stroke: currentColor;
  stroke-width: 1.9;
  stroke-linecap: round;
  stroke-linejoin: round;
  pointer-events: none;
}

#page-indicator {
  font-size: 13px;
  color: var(--text-muted);
  font-variant-numeric: tabular-nums;
  display: flex;
  align-items: center;
  gap: var(--space-1);
  line-height: var(--line-height-tight);
}

#page-input {
  width: 8ch;
  text-align: center;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 3px 4px;
  font-size: 13px;
  background: var(--bg-surface);
  color: var(--text);
  font-variant-numeric: tabular-nums;
  -moz-appearance: textfield;
}

#page-input::-webkit-inner-spin-button,
#page-input::-webkit-outer-spin-button {
  -webkit-appearance: none;
}

#page-total {
  margin-left: 1ch;
}

/* Main area */
#main-area {
  display: flex;
  flex: 1;
  overflow: hidden;
  position: relative;
}

/* Thumbnail panel */
#thumbnail-panel {
  width: 180px;
  flex-shrink: 0;
  overflow-y: auto;
  overflow-anchor: none;
  background: var(--bg-panel);
  border-right: 1px solid var(--border);
  padding: var(--space-3) var(--space-2);
  transition: margin-left var(--transition);
}

#thumbnail-panel.is-scrubbing {
  scroll-behavior: auto;
}

#thumbnail-panel.hidden {
  margin-left: -180px;
  visibility: hidden;
}

#thumbnail-list {
  display: flex;
  flex-direction: column;
  gap: var(--space-2);
}

.thumb-item {
  cursor: pointer;
  border-radius: var(--radius-md);
  overflow: hidden;
  border: 2px solid transparent;
  transition: border-color var(--transition), box-shadow var(--transition);
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  aspect-ratio: 9 / 16;
  background: var(--bg-surface);
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
}

.thumb-item:hover {
  border-color: var(--border);
  box-shadow: var(--shadow);
}

.thumb-item.active {
  border-color: var(--thumb-active);
  box-shadow: 0 0 0 1px var(--thumb-active);
}

.thumb-item img {
  width: 100%;
  height: 100%;
  display: block;
  object-fit: contain;
  background: var(--bg-surface);
  color: transparent;
  font-size: 0;
}

.thumb-item img:not([src]),
.thumb-item img.image-load-failed {
  visibility: hidden;
}

.thumb-label {
  position: absolute;
  bottom: var(--space-1);
  right: var(--space-1);
  background: rgba(0,0,0,0.6);
  color: #fff;
  font-size: 10px;
  padding: 1px 5px;
  border-radius: var(--radius-sm);
  font-variant-numeric: tabular-nums;
}

/* Viewer */
#viewer {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  overflow: hidden;
  position: relative;
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
  -webkit-tap-highlight-color: transparent;
}

#page-container {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: auto;
  width: 100%;
  padding: var(--space-6);
  position: relative;
  overscroll-behavior: contain;
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
  -webkit-tap-highlight-color: transparent;
}

#page-image {
  width: 100%;
  height: 100%;
  max-width: 100%;
  max-height: 100%;
  object-fit: contain;
  box-shadow: var(--shadow-lg);
  border-radius: 2px;
  background: var(--bg-surface);
  transition: opacity 120ms ease;
  user-select: none;
  -webkit-user-select: none;
  -webkit-user-drag: none;
  -webkit-touch-callout: none;
  pointer-events: none;
}

#page-image:not([src]) {
  visibility: hidden;
}

#page-image.image-load-failed {
  visibility: hidden;
}

#page-image.loading {
  opacity: 1;
}

#page-image.thumb-fallback {
  object-fit: contain;
}

/* Loading indicator */
#loading-indicator {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  pointer-events: none;
}

#loading-indicator.hidden { display: none; }

.spinner {
  width: 28px;
  height: 28px;
  border: 2.5px solid var(--border);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.7s linear infinite;
}

@keyframes spin {
  to { transform: rotate(360deg); }
}

/* Play controls (video mode) / Scrub bar (all modes) */
#play-controls {
  display: flex;
  align-items: center;
  gap: var(--space-3);
  padding: 10px var(--space-5);
  background: var(--toolbar-bg);
  backdrop-filter: blur(12px);
  border-top: 1px solid var(--border);
  width: 100%;
  flex-shrink: 0;
}

#play-controls.hidden { display: none; }

html:not(.mode-video) #btn-play,
html:not(.mode-video) #btn-low-quality,
html:not(.mode-video) #time-display {
  display: none;
}

#play-controls button {
  background: none;
  border: none;
  color: var(--text);
  cursor: pointer;
  font-size: 16px;
  padding: var(--space-1) var(--space-2);
  min-width: 36px;
  min-height: 36px;
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
}

#btn-low-quality {
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  font-size: 16px;
  min-width: 36px;
  padding: 0 var(--space-2);
  white-space: nowrap;
}

#btn-low-quality[aria-pressed="true"] {
  background: color-mix(in srgb, var(--accent) 18%, transparent);
  border-color: color-mix(in srgb, var(--accent) 55%, var(--border));
  color: var(--accent);
}

#scrubber {
  flex: 1;
  height: 4px;
  min-height: 32px;
  -webkit-appearance: none;
  appearance: none;
  background: var(--border);
  border-radius: 2px;
  outline: none;
  cursor: pointer;
  touch-action: pan-x;
}

#scrubber::-webkit-slider-thumb {
  -webkit-appearance: none;
  width: 14px;
  height: 14px;
  background: var(--accent);
  border-radius: 50%;
  cursor: pointer;
}

#scrubber::-moz-range-thumb {
  width: 14px;
  height: 14px;
  background: var(--accent);
  border: none;
  border-radius: 50%;
  cursor: pointer;
}

#time-display {
  font-size: 12px;
  color: var(--text-muted);
  font-variant-numeric: tabular-nums;
  min-width: 40px;
}

/* Branding footer */
#branding {
  text-align: center;
  padding: 6px;
  font-size: 11px;
  color: var(--text-muted);
  border-top: 1px solid var(--border);
  flex-shrink: 0;
  line-height: var(--line-height-tight);
}

.branding-lockup {
  display: inline-flex;
  align-items: center;
  gap: var(--space-2);
}

.branding-lockup img {
  width: 16px;
  height: 16px;
  border-radius: 4px;
  flex-shrink: 0;
}

#branding a {
  color: var(--text-muted);
  text-decoration: none;
  transition: color var(--transition);
}

#branding a:is(:hover, :focus-visible) {
  color: var(--accent);
}

#toolbar button:focus-visible,
#play-controls button:focus-visible,
#page-input:focus-visible,
#scrubber:focus-visible,
#jump-dialog input:focus-visible,
#jump-dialog button:focus-visible,
#controls-help-close:focus-visible,
#branding a:focus-visible {
  outline: 2px solid var(--focus-ring);
  outline-offset: 2px;
  box-shadow: 0 0 0 4px var(--focus-halo);
}

/* Jump dialog */
#jump-dialog {
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  padding: var(--space-5);
  background: var(--bg-surface);
  color: var(--text);
  box-shadow: var(--shadow-lg);
}

#jump-dialog::backdrop {
  background: rgba(0,0,0,0.3);
}

#jump-dialog label {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  font-size: 14px;
  line-height: var(--line-height-tight);
}

#jump-dialog input {
  width: 72px;
  padding: 6px var(--space-2);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  font-size: 14px;
  background: var(--bg);
  color: var(--text);
}

#jump-dialog button {
  margin-left: var(--space-3);
  padding: 6px var(--space-4);
  background: var(--accent);
  color: #fff;
  border: none;
  border-radius: var(--radius-sm);
  cursor: pointer;
  font-size: 13px;
}

#btn-help {
  font-weight: 700;
}

#controls-dialog {
  position: fixed;
  inset: 0;
  margin: auto;
  width: min(560px, 80vw);
  max-width: 80vw;
  max-height: 80vh;
  padding: 0;
  border: 1px solid rgba(0, 168, 214, 0.86);
  border-radius: 4px;
  background:
    linear-gradient(90deg, rgba(0, 214, 255, 0.16) 0 1px, transparent 1px calc(100% - 1px), rgba(0, 214, 255, 0.16) calc(100% - 1px)),
    linear-gradient(rgba(0, 214, 255, 0.16) 0 1px, transparent 1px calc(100% - 1px), rgba(0, 214, 255, 0.16) calc(100% - 1px)),
    rgba(220, 251, 255, 0.96);
  color: #031b24;
  box-shadow: 0 0 0 1px rgba(0, 44, 56, 0.18), 0 12px 36px rgba(0, 18, 26, 0.32);
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
}

html.dark #controls-dialog {
  border-color: rgba(0, 216, 255, 0.88);
  background:
    linear-gradient(90deg, rgba(0, 216, 255, 0.18) 0 1px, transparent 1px calc(100% - 1px), rgba(0, 216, 255, 0.18) calc(100% - 1px)),
    linear-gradient(rgba(0, 216, 255, 0.18) 0 1px, transparent 1px calc(100% - 1px), rgba(0, 216, 255, 0.18) calc(100% - 1px)),
    rgba(0, 16, 24, 0.96);
  color: #dffaff;
}

#controls-dialog::backdrop {
  background: rgba(0, 8, 12, 0.52);
  backdrop-filter: blur(3px);
  -webkit-backdrop-filter: blur(3px);
}

.controls-help {
  display: flex;
  flex-direction: column;
  height: min(720px, 80vh);
  max-height: 80vh;
  font-family: "SFMono-Regular", "Roboto Mono", ui-monospace, monospace;
  letter-spacing: 0.03em;
}

.controls-help-header {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 12px;
  padding: 12px 14px 10px;
  border-bottom: 1px solid rgba(0, 99, 134, 0.34);
}

html.dark .controls-help-header {
  border-bottom-color: rgba(70, 223, 255, 0.24);
}

.controls-help-headline {
  min-width: 0;
}

.controls-help-pills {
  display: flex;
  flex-wrap: wrap;
  gap: 4px;
  margin-bottom: 4px;
}

.controls-help-pills:empty {
  display: none;
}

.controls-help-pill,
.controls-help-key,
#controls-help-close {
  display: inline-flex;
  align-items: center;
  min-height: 0;
  padding: 1px 6px;
  border: 1px solid rgba(0, 99, 134, 0.5);
  border-radius: 2px;
  background: rgba(255, 255, 255, 0.68);
  color: #00384b;
  font: inherit;
  font-size: 10px;
  line-height: 1.3;
}

html.dark .controls-help-pill,
html.dark .controls-help-key,
html.dark #controls-help-close {
  border-color: rgba(70, 223, 255, 0.32);
  background: rgba(0, 43, 59, 0.86);
  color: #7eefff;
}

#controls-help-title {
  margin: 0;
  font-size: 13px;
  line-height: 1.08;
  letter-spacing: 0.03em;
}

#controls-help-close {
  cursor: pointer;
}

.controls-help-body {
  overflow-x: hidden;
  overflow-y: scroll;
  scrollbar-gutter: stable;
  min-height: 0;
  padding: 12px 14px 14px;
  display: grid;
  gap: 12px;
}

.controls-help-section h3 {
  margin: 0 0 6px;
  font-size: 10px;
  line-height: 1.1;
  letter-spacing: 0.07em;
  color: #00526e;
  text-transform: uppercase;
}

html.dark .controls-help-section h3 {
  color: #46dfff;
}

.controls-help-section dl {
  margin: 0;
  display: grid;
  grid-template-columns: auto minmax(0, 1fr);
  gap: 5px 10px;
}

.controls-help-section div {
  display: contents;
}

.controls-help-section dt {
  margin: 0;
  color: #00526e;
  font-size: 9px;
  line-height: 1.15;
  letter-spacing: 0.05em;
  text-transform: uppercase;
}

html.dark .controls-help-section dt {
  color: #46dfff;
}

.controls-help-section dd {
  margin: 0;
  font-size: 10px;
  line-height: 1.35;
  overflow-wrap: anywhere;
}

/* Responsive */
@media (max-width: 640px) {
  #toolbar {
    height: auto;
    padding: 10px var(--space-3);
    gap: var(--space-2) 10px;
    flex-wrap: wrap;
    align-items: center;
    justify-content: center;
  }
  .toolbar-left {
    flex: 1 1 100%;
    text-align: center;
  }
  .toolbar-center {
    flex: 1 1 100%;
    min-width: 0;
    justify-content: center;
  }
  .toolbar-right {
    flex: 1 1 100%;
    gap: var(--space-1);
    justify-content: center;
  }
  .title {
    font-size: 13px;
    white-space: normal;
    display: -webkit-box;
    -webkit-box-orient: vertical;
    -webkit-line-clamp: 2;
    overflow: hidden;
  }
  #toolbar button {
    min-width: 40px;
    min-height: 40px;
    padding: var(--space-2);
  }
  #page-input {
    width: 9ch;
    min-height: 36px;
    font-size: 14px;
  }
  #thumbnail-panel {
    position: absolute;
    top: 0;
    bottom: 0;
    left: 0;
    width: min(148px, 44vw);
    z-index: 20;
    box-shadow: var(--shadow-lg);
    transition: transform var(--transition), visibility var(--transition);
  }
  #thumbnail-panel.hidden {
    margin-left: 0;
    transform: translateX(-100%);
  }
  #page-container { padding: 10px; }
  #play-controls {
    flex-wrap: wrap;
    gap: 4px 10px;
    padding: 4px var(--space-3) 8px;
  }
  #play-controls button {
    min-width: 34px;
    min-height: 28px;
    padding: 2px 6px;
    font-size: 14px;
  }
  #btn-low-quality {
    font-size: 14px;
    min-width: 34px;
    padding-inline: 6px;
  }
  #scrubber {
    order: 3;
    flex-basis: 100%;
    min-height: 36px;
  }
  #time-display {
    min-width: 48px;
    margin-left: auto;
    text-align: right;
    line-height: 1;
  }
  #controls-dialog {
    width: 80vw;
    max-width: 80vw;
  }
  .controls-help-header,
  .controls-help-body {
    padding-left: 10px;
    padding-right: 10px;
  }
  .controls-help-header {
    padding-top: 10px;
    padding-bottom: 8px;
  }
  .controls-help-body {
    gap: 10px;
    padding-top: 10px;
    padding-bottom: 10px;
  }
  #controls-help-title {
    font-size: 11px;
  }
  .controls-help-pill,
  .controls-help-key,
  #controls-help-close,
  .controls-help-section dd {
    font-size: 9px;
  }
  .controls-help-section h3,
  .controls-help-section dt {
    font-size: 8px;
  }
  .controls-help-section dl {
    gap: 4px 8px;
  }
  #branding { display: none; }
}

@media (max-width: 480px) {
  .title {
    -webkit-line-clamp: 1;
  }
  #thumbnail-panel {
    width: min(132px, 48vw);
  }
  #page-container { padding: 8px; }
}

@media (pointer: coarse) {
  #scrubber {
    height: 6px;
    min-height: 40px;
  }
  #scrubber::-webkit-slider-thumb {
    width: 24px;
    height: 24px;
  }
  #scrubber::-moz-range-thumb {
    width: 24px;
    height: 24px;
  }
}

@media (max-width: 640px) and (orientation: portrait) {
  #thumbnail-panel {
    position: absolute;
    top: auto;
    right: 0;
    bottom: 80px;
    left: 0;
    width: auto;
    height: 96px;
    overflow-x: auto;
    overflow-y: hidden;
    padding: 6px 10px;
    border-top: 1px solid var(--border);
    border-right: 0;
    box-shadow: 0 -4px 14px rgba(0,0,0,0.12);
    transition: transform var(--transition), visibility var(--transition);
    -webkit-overflow-scrolling: touch;
    overscroll-behavior-x: contain;
  }

  #thumbnail-panel.hidden {
    margin-left: 0;
    transform: translateY(calc(100% + 8px));
    visibility: hidden;
  }

  #thumbnail-list {
    flex-direction: row;
    align-items: stretch;
    height: 100%;
    gap: 8px;
  }

  .thumb-virtual-spacer {
    flex: 0 0 auto;
    height: 100%;
  }

  .thumb-item {
    flex: 0 0 auto;
    height: 100%;
    min-width: 52px;
  }

  .thumb-label {
    font-size: 9px;
    padding: 1px 4px;
  }

  #main-area:has(#thumbnail-panel:not(.hidden)) #page-container {
    padding-bottom: 108px;
  }
}

/* Fullscreen */
:fullscreen #toolbar { display: none; }
:fullscreen #branding { display: none; }
:fullscreen #page-container { padding: 0; }
:fullscreen body { background: #000; }
:fullscreen #page-image { width: 100vw; height: 100vh; max-height: 100vh; border-radius: 0; box-shadow: none; }

html.is-fallback-fullscreen,
body.is-fallback-fullscreen {
  width: 100%;
  height: 100%;
  overflow: hidden;
  background: #000;
}

html.is-fallback-fullscreen #app {
  position: fixed;
  inset: 0;
  z-index: 2147483647;
  width: 100vw;
  height: 100vh;
  height: 100dvh;
  background: #000;
}

html.is-fallback-fullscreen #branding {
  display: none;
}

html.is-fallback-fullscreen #page-container {
  padding: 0;
  background: #000;
}

html.is-fallback-fullscreen #page-image {
  width: 100%;
  height: 100%;
  max-height: 100%;
  border-radius: 0;
  box-shadow: none;
}
