/*
 * OpenLucida — Free, open-source camera lucida for artists
 * Copyright (C) 2025 JPG Tech Solutions
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <https://www.gnu.org/licenses/>.
 */

:root {
  --bg-primary: #1a1a2e;
  --bg-toolbar: #16213e;
  --text-color: #e0e0e0;
  --text-muted: #8a8a9a;
  --accent: #0f97b5;
  --accent-hover: #0db8d9;
  --toolbar-height: 64px;
  --danger: #e74c3c;
}

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

html, body {
  height: 100dvh;
  overflow: hidden;
  background: var(--bg-primary);
  color: var(--text-color);
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', system-ui, sans-serif;
  -webkit-font-smoothing: antialiased;
}

/* ---- Main Screen ---- */
#main-screen {
  position: relative;
  width: 100%;
  height: 100dvh;
}

#camera-feed {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  object-fit: cover;
  background: #000;
  z-index: 1;
}

/* Overlay image — reference image displayed over the camera feed */
#overlay-image {
  position: absolute;
  top: 50%;
  left: 50%;
  max-width: 100%;
  max-height: 100%;
  transform-origin: center center;
  z-index: 2;
  pointer-events: none;
  touch-action: none;
  will-change: transform, opacity;
  object-fit: contain;
}

#overlay-image[hidden] {
  display: none;
}

/* Overlay canvas (future) */
#overlay-canvas {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  z-index: 2;
  pointer-events: none;
  touch-action: none;
  will-change: transform, opacity;
}

/* Grid canvas (future) */
#grid-canvas {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  z-index: 3;
  pointer-events: none;
}

/* Gesture layer — captures all touch input above overlay */
#gesture-layer {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  z-index: 4;
  touch-action: none;
}

/* ---- Floating record button ---- */
.floating-record-btn {
  position: fixed;
  top: 16px;
  right: 16px;
  z-index: 9;
  background: rgba(0, 0, 0, 0.45);
  border: 2px solid rgba(255, 255, 255, 0.4);
  width: 44px;
  height: 44px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  backdrop-filter: blur(4px);
  -webkit-backdrop-filter: blur(4px);
}

.floating-record-btn[hidden] {
  display: none;
}

.floating-record-btn:active {
  background: rgba(0, 0, 0, 0.65);
}

.record-dot {
  width: 18px;
  height: 18px;
  background: var(--danger);
  border-radius: 50%;
  transition: border-radius 200ms ease, width 200ms ease, height 200ms ease;
}

.floating-record-btn.recording {
  border-color: var(--danger);
}

.floating-record-btn.recording .record-dot {
  width: 14px;
  height: 14px;
  border-radius: 3px;
}

@supports (padding-top: env(safe-area-inset-top)) {
  .floating-record-btn {
    top: calc(16px + env(safe-area-inset-top));
  }
}

/* ---- Toolbar ---- */
#toolbar {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  height: var(--toolbar-height);
  background: var(--bg-toolbar);
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 12px;
  padding: 0 16px;
  z-index: 10;
  transition: transform 200ms ease-out;
}

#toolbar.toolbar-hidden {
  transform: translateY(100%);
}

.toolbar-btn {
  background: none;
  border: none;
  color: var(--text-color);
  font-size: 1.4rem;
  padding: 8px;
  border-radius: 8px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  min-width: 44px;
  min-height: 44px;
}

.toolbar-btn[hidden] {
  display: none;
}

.toolbar-btn:active {
  background: rgba(255, 255, 255, 0.1);
}

/* Image controls popover */
.image-popover {
  position: fixed;
  bottom: calc(var(--toolbar-height) + 12px);
  left: 16px;
  right: 16px;
  background: var(--bg-toolbar);
  border-radius: 12px;
  padding: 14px 16px;
  z-index: 11;
  box-shadow: 0 -2px 16px rgba(0, 0, 0, 0.4);
}

.image-popover[hidden] {
  display: none;
}

.popover-row {
  display: flex;
  align-items: center;
  gap: 12px;
  margin-bottom: 12px;
}

.popover-label {
  font-size: 0.8rem;
  color: var(--text-muted);
  white-space: nowrap;
  min-width: 52px;
}

.popover-slider {
  -webkit-appearance: none;
  appearance: none;
  flex: 1;
  height: 4px;
  background: rgba(255, 255, 255, 0.2);
  border-radius: 2px;
  outline: none;
}

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

.popover-tools {
  display: flex;
  gap: 4px;
  justify-content: space-around;
}

.popover-tool-btn {
  background: none;
  border: none;
  color: var(--text-muted);
  padding: 6px 4px;
  cursor: pointer;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
  border-radius: 8px;
  min-width: 44px;
  min-height: 44px;
}

.popover-tool-btn:active {
  background: rgba(255, 255, 255, 0.1);
}

.popover-tool-label {
  font-size: 0.65rem;
  white-space: nowrap;
}

.popover-tool-danger {
  color: var(--danger);
}

.toolbar-btn.active {
  background: rgba(15, 151, 181, 0.2);
  color: var(--accent);
}

/* Lock button icon toggle */
#lock-btn .icon-locked { display: none; }
#lock-btn[aria-pressed="true"] .icon-unlocked { display: none; }
#lock-btn[aria-pressed="true"] .icon-locked { display: block; }
#lock-btn[aria-pressed="true"] { color: var(--accent); }

/* ---- Overlay Screens (Pre-Permission, Denied) ---- */
.overlay-screen {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100dvh;
  background: var(--bg-primary);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 32px;
  text-align: center;
  z-index: 20;
}

.overlay-screen[hidden] {
  display: none;
}

.screen-icon {
  font-size: 4rem;
  margin-bottom: 24px;
  opacity: 0.8;
}

.screen-title {
  font-size: 1.25rem;
  font-weight: 600;
  margin-bottom: 12px;
  line-height: 1.4;
}

.screen-subtitle {
  font-size: 0.95rem;
  color: var(--text-muted);
  margin-bottom: 32px;
  max-width: 320px;
  line-height: 1.5;
}

.btn-primary {
  background: var(--accent);
  color: #fff;
  border: none;
  padding: 14px 32px;
  font-size: 1rem;
  font-weight: 600;
  border-radius: 12px;
  cursor: pointer;
  min-width: 200px;
  min-height: 48px;
}

.btn-primary:active {
  background: var(--accent-hover);
}

.screen-help {
  margin-top: 24px;
  font-size: 0.85rem;
  color: var(--text-muted);
  max-width: 300px;
  line-height: 1.5;
}

/* ---- Permission Denied specifics ---- */
.instructions {
  text-align: left;
  font-size: 0.9rem;
  color: var(--text-muted);
  margin-bottom: 24px;
  max-width: 320px;
  line-height: 1.6;
}

.instructions strong {
  color: var(--text-color);
}

.instruction-step {
  padding: 8px 0;
  border-bottom: 1px solid rgba(255, 255, 255, 0.06);
}

.instruction-step:last-child {
  border-bottom: none;
}

/* ---- Filter picker panel ---- */
.filter-picker {
  position: fixed;
  bottom: calc(var(--toolbar-height) + 12px);
  left: 16px;
  right: 16px;
  background: var(--bg-toolbar);
  border-radius: 12px;
  padding: 8px;
  z-index: 11;
  box-shadow: 0 -2px 16px rgba(0, 0, 0, 0.4);
  display: flex;
  gap: 6px;
  flex-wrap: wrap;
}

.filter-picker[hidden] {
  display: none;
}

.filter-picker-option {
  background: none;
  border: 1px solid rgba(255, 255, 255, 0.1);
  color: var(--text-color);
  padding: 8px 14px;
  border-radius: 8px;
  font-size: 0.85rem;
  cursor: pointer;
  flex: 1 1 auto;
  text-align: center;
  min-width: 0;
}

.filter-picker-option:active {
  background: rgba(15, 151, 181, 0.2);
}

.filter-picker-option[aria-checked="true"] {
  background: rgba(15, 151, 181, 0.2);
  color: var(--accent);
  border-color: var(--accent);
}

/* ---- Filter toast ---- */
.filter-toast {
  position: fixed;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  background: rgba(0, 0, 0, 0.75);
  color: #fff;
  padding: 10px 24px;
  border-radius: 8px;
  font-size: 0.95rem;
  font-weight: 500;
  z-index: 12;
  pointer-events: none;
  transition: opacity 300ms ease-out;
}

.filter-toast[hidden] {
  display: none;
}

.filter-toast.toast-fade-out {
  opacity: 0;
}

/* ---- Grid overlay (rule of thirds) ---- */
.grid-overlay {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  z-index: 3;
  pointer-events: none;
  background:
    linear-gradient(to right, transparent calc(33.333% - 0.5px), rgba(255, 255, 255, 0.35) calc(33.333% - 0.5px), rgba(255, 255, 255, 0.35) calc(33.333% + 0.5px), transparent calc(33.333% + 0.5px),
      transparent calc(66.666% - 0.5px), rgba(255, 255, 255, 0.35) calc(66.666% - 0.5px), rgba(255, 255, 255, 0.35) calc(66.666% + 0.5px), transparent calc(66.666% + 0.5px)),
    linear-gradient(to bottom, transparent calc(33.333% - 0.5px), rgba(255, 255, 255, 0.35) calc(33.333% - 0.5px), rgba(255, 255, 255, 0.35) calc(33.333% + 0.5px), transparent calc(33.333% + 0.5px),
      transparent calc(66.666% - 0.5px), rgba(255, 255, 255, 0.35) calc(66.666% - 0.5px), rgba(255, 255, 255, 0.35) calc(66.666% + 0.5px), transparent calc(66.666% + 0.5px));
}

.grid-overlay[hidden] {
  display: none;
}

/* ---- Record mode prompt ---- */
.record-prompt {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100dvh;
  background: rgba(0, 0, 0, 0.7);
  display: flex;
  align-items: flex-end;
  justify-content: center;
  z-index: 20;
  padding: 24px 16px;
}

.record-prompt[hidden] {
  display: none;
}

.record-prompt-card {
  background: var(--bg-toolbar);
  border-radius: 16px;
  padding: 20px 16px;
  width: 100%;
  max-width: 340px;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.record-prompt-title {
  font-size: 1rem;
  font-weight: 600;
  text-align: center;
  margin-bottom: 4px;
}

.record-prompt-option {
  background: none;
  border: 1px solid rgba(255, 255, 255, 0.12);
  color: var(--text-color);
  padding: 12px 16px;
  border-radius: 10px;
  font-size: 0.95rem;
  cursor: pointer;
  display: flex;
  align-items: center;
  gap: 10px;
}

.record-prompt-option:active {
  background: rgba(15, 151, 181, 0.2);
  border-color: var(--accent);
}

.record-prompt-cancel {
  background: none;
  border: none;
  color: var(--text-muted);
  padding: 10px;
  font-size: 0.9rem;
  cursor: pointer;
  text-align: center;
  margin-top: 4px;
}

/* ---- Grid / Filter button active states ---- */
#grid-btn[aria-pressed="true"] { color: var(--accent); }
#filter-btn.filter-active { color: var(--accent); }

/* ---- Onboarding overlay ---- */
.onboarding-backdrop {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100dvh;
  background: rgba(0, 0, 0, 0.8);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 30;
  padding: 24px;
}

.onboarding-card {
  background: var(--bg-toolbar);
  border-radius: 16px;
  padding: 32px 24px 24px;
  max-width: 340px;
  width: 100%;
  text-align: center;
  position: relative;
}

.onboarding-skip {
  position: absolute;
  top: 12px;
  right: 16px;
  background: none;
  border: none;
  color: var(--text-muted);
  font-size: 0.85rem;
  cursor: pointer;
  padding: 4px 8px;
}

.onboarding-icon {
  margin-bottom: 20px;
  opacity: 0.8;
}

.onboarding-title {
  font-size: 1.1rem;
  font-weight: 600;
  margin-bottom: 8px;
  line-height: 1.4;
}

.onboarding-text {
  font-size: 0.9rem;
  color: var(--text-muted);
  line-height: 1.5;
  margin-bottom: 24px;
}

.onboarding-dots {
  display: flex;
  justify-content: center;
  gap: 8px;
  margin-bottom: 20px;
}

.onboarding-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.2);
}

.onboarding-dot.active {
  background: var(--accent);
}

.onboarding-action {
  width: 100%;
}

/* ---- iOS Add to Home Screen banner ---- */
.ios-banner {
  position: fixed;
  bottom: calc(var(--toolbar-height) + 16px);
  left: 12px;
  right: 12px;
  background: var(--bg-toolbar);
  border-radius: 12px;
  padding: 12px 16px;
  display: flex;
  align-items: center;
  gap: 12px;
  z-index: 15;
  box-shadow: 0 2px 16px rgba(0, 0, 0, 0.4);
}

.ios-banner-text {
  flex: 1;
  font-size: 0.85rem;
  color: var(--text-color);
  line-height: 1.4;
}

.ios-share-icon {
  display: inline-block;
  vertical-align: middle;
  color: var(--accent);
}

.ios-banner-close {
  background: none;
  border: none;
  color: var(--text-muted);
  font-size: 1.4rem;
  cursor: pointer;
  padding: 4px 8px;
  line-height: 1;
}

/* ---- Update banner ---- */
.update-banner {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  background: var(--accent);
  color: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 12px;
  padding: 10px 16px;
  font-size: 0.9rem;
  z-index: 50;
}

.update-banner-btn {
  background: rgba(255, 255, 255, 0.2);
  border: none;
  color: #fff;
  padding: 4px 12px;
  border-radius: 6px;
  font-size: 0.85rem;
  cursor: pointer;
  font-weight: 600;
}

/* ---- Resume session prompt ---- */
.resume-prompt {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100dvh;
  background: rgba(0, 0, 0, 0.8);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 25;
  padding: 24px;
}

.resume-card {
  background: var(--bg-toolbar);
  border-radius: 16px;
  padding: 28px 24px;
  max-width: 320px;
  width: 100%;
  text-align: center;
}

.resume-title {
  font-size: 1.1rem;
  font-weight: 600;
  margin-bottom: 8px;
}

.resume-text {
  font-size: 0.9rem;
  color: var(--text-muted);
  margin-bottom: 24px;
}

.resume-actions {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.resume-no {
  background: none;
  border: 1px solid rgba(255, 255, 255, 0.15);
  color: var(--text-color);
  padding: 12px 24px;
  font-size: 1rem;
  border-radius: 12px;
  cursor: pointer;
}

/* ---- Desktop gate ---- */
.desktop-gate {
  min-height: 100dvh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 32px;
  background: var(--bg-primary);
}

.desktop-gate-card {
  text-align: center;
  max-width: 420px;
}

.desktop-gate-title {
  font-size: 2rem;
  font-weight: 700;
  margin-bottom: 16px;
}

.desktop-gate-desc {
  font-size: 1rem;
  color: var(--text-muted);
  line-height: 1.6;
  margin-bottom: 32px;
}

.desktop-gate-qr {
  margin-bottom: 16px;
}

.desktop-gate-qr canvas {
  border-radius: 8px;
}

.desktop-gate-url {
  font-size: 0.75rem;
  color: var(--text-muted);
  word-break: break-all;
  margin-top: 8px;
}

.desktop-gate-hint {
  font-size: 0.95rem;
  color: var(--text-color);
  margin-bottom: 32px;
}

.desktop-gate-footer {
  font-size: 0.8rem;
  color: var(--text-muted);
}

/* ---- Android install banner ---- */
.install-banner {
  position: fixed;
  bottom: calc(var(--toolbar-height) + 16px);
  left: 12px;
  right: 12px;
  background: var(--bg-toolbar);
  border-radius: 12px;
  padding: 12px 16px;
  display: flex;
  align-items: center;
  gap: 12px;
  z-index: 15;
  box-shadow: 0 2px 16px rgba(0, 0, 0, 0.4);
}

.install-banner-text {
  flex: 1;
  font-size: 0.85rem;
  color: var(--text-color);
}

.install-banner-btn {
  background: var(--accent);
  border: none;
  color: #fff;
  padding: 6px 14px;
  border-radius: 8px;
  font-size: 0.85rem;
  font-weight: 600;
  cursor: pointer;
}

.install-banner-close {
  background: none;
  border: none;
  color: var(--text-muted);
  font-size: 1.4rem;
  cursor: pointer;
  padding: 4px 8px;
  line-height: 1;
}

/* ---- Accessibility ---- */
@media (prefers-reduced-motion: reduce) {
  * {
    transition: none !important;
    animation: none !important;
  }
}

/* ---- Safe area (notched devices) ---- */
@supports (padding-bottom: env(safe-area-inset-bottom)) {
  #toolbar {
    padding-bottom: env(safe-area-inset-bottom);
    height: calc(var(--toolbar-height) + env(safe-area-inset-bottom));
  }
  .image-popover,
  .filter-picker {
    bottom: calc(var(--toolbar-height) + env(safe-area-inset-bottom) + 12px);
  }
  .ios-banner {
    bottom: calc(var(--toolbar-height) + env(safe-area-inset-bottom) + 16px);
  }
}
