:root {
  --bg: #1b1c1f;
  --viewport: #141517;
  --panel: #232428;
  --border: #33353a;
  --text: #e8e8ea;
  --muted: #9a9ca3;
  --accent: #4f8cff;
  --accent-hover: #6a9dff;
  --overlay: rgba(79, 140, 255, 0.45);
  color-scheme: dark;
}

* {
  box-sizing: border-box;
}

[hidden] {
  display: none !important;
}

html,
body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font: 13px/1.45 -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
  user-select: none;
  overflow: hidden;
}

.app {
  display: grid;
  grid-template-columns: 1fr 280px;
  height: 100vh;
  height: 100dvh; /* Safari's toolbars never cover the controls */
}

/* ---------- Viewport ---------- */

.viewport {
  position: relative;
  background: var(--viewport);
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
  padding: 24px;
  /* Pinch-zoom and pan are handled by the editor, not the page. */
  touch-action: none;
}

.stage {
  position: relative;
  flex: none;
  box-shadow: 0 6px 30px rgba(0, 0, 0, 0.45);
  -webkit-app-region: no-drag;
}

.canvas {
  display: block;
  cursor: crosshair;
}

.stage.brush .canvas {
  cursor: none;
}

.stage.no-pick .canvas {
  cursor: default;
}

.overlay {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
}

.viewport.drag-over::after {
  content: '';
  position: absolute;
  inset: 12px;
  border: 2px dashed var(--accent);
  border-radius: 12px;
  pointer-events: none;
}

.dropzone {
  -webkit-app-region: no-drag;
}

.dropzone-inner {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
  padding: 48px 64px;
  border: 1.5px dashed var(--border);
  border-radius: 14px;
  color: var(--muted);
}

.dropzone-icon {
  width: 44px;
  height: 44px;
  fill: currentColor;
  opacity: 0.7;
}

.dropzone-title {
  margin: 8px 0 0;
  font-size: 15px;
  color: var(--text);
}

.dropzone-sub {
  margin: 0 0 6px;
}

.zoom-bar {
  position: absolute;
  right: 14px;
  bottom: 14px;
  display: flex;
  align-items: center;
  gap: 2px;
  padding: 2px;
  background: rgba(20, 21, 23, 0.88);
  border: 1px solid var(--border);
  border-radius: 8px;
  backdrop-filter: blur(8px);
  -webkit-app-region: no-drag;
}

.zoom-btn {
  appearance: none;
  display: flex;
  align-items: center;
  justify-content: center;
  height: 24px;
  min-width: 26px;
  padding: 0 6px;
  border: 0;
  border-radius: 6px;
  background: transparent;
  color: var(--text);
  font: inherit;
  font-size: 12px;
  font-variant-numeric: tabular-nums;
  cursor: pointer;
}

.zoom-btn:hover:not(:disabled) {
  background: #36383e;
}

.zoom-btn:disabled {
  opacity: 0.4;
  cursor: default;
}

.zoom-btn svg {
  width: 12px;
  height: 12px;
  fill: currentColor;
}

.zoom-level {
  min-width: 50px;
}

.stage.panning .canvas {
  cursor: grabbing;
}

.toast {
  position: absolute;
  bottom: 20px;
  left: 50%;
  transform: translateX(-50%);
  max-width: min(560px, calc(100% - 48px));
  padding: 9px 16px;
  background: rgba(20, 21, 23, 0.92);
  border: 1px solid var(--border);
  border-radius: 10px;
  backdrop-filter: blur(8px);
  text-align: center;
  -webkit-app-region: no-drag;
}

.toast.error {
  border-color: #8a4b3c;
  color: #f2c0b3;
}

.handover {
  padding: 10px 12px;
  border: 1px solid rgba(79, 140, 255, 0.45);
  border-radius: 8px;
  background: rgba(79, 140, 255, 0.1);
}

.handover .hint {
  margin-bottom: 8px;
  color: var(--text);
}

.handover .btn-block {
  margin-top: 0;
}

.row-tight {
  margin-bottom: 0;
}

/* Export / Save to Photos share the sidebar width: keep each label on one line. */
.row-tight .btn {
  padding-inline: 8px;
  white-space: nowrap;
}

.busy {
  position: absolute;
  bottom: 20px;
  left: 50%;
  transform: translateX(-50%);
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 14px;
  background: rgba(20, 21, 23, 0.85);
  border: 1px solid var(--border);
  border-radius: 999px;
  backdrop-filter: blur(8px);
}

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

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

/* ---------- Sidebar ---------- */

.sidebar {
  display: flex;
  flex-direction: column;
  min-height: 0;
  background: var(--panel);
  border-left: 1px solid var(--border);
}

.sidebar-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 16px;
  border-bottom: 1px solid var(--border);
}

.sidebar-header .btn {
  -webkit-app-region: no-drag;
}

h1 {
  margin: 0;
  font-size: 15px;
  font-weight: 600;
}

h2 {
  margin: 0 0 8px;
  font-size: 11px;
  font-weight: 600;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: var(--muted);
}

.sidebar-scroll {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
}

.panel {
  border-bottom: 1px solid var(--border);
}

/* Collapsible section header: the whole row toggles the section. */
.panel-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 16px;
  list-style: none;
  cursor: pointer;
}

.panel-header::-webkit-details-marker {
  display: none;
}

.panel-header h2 {
  margin: 0;
}

.panel-header::after {
  content: '';
  width: 6px;
  height: 6px;
  margin-right: 3px;
  border-right: 1.5px solid var(--muted);
  border-bottom: 1.5px solid var(--muted);
  transform: translateY(-2px) rotate(45deg); /* chevron pointing down: open */
  transition: transform 0.15s ease;
}

.panel:not([open]) > .panel-header::after {
  transform: rotate(-45deg); /* pointing right: collapsed */
}

.panel-header:hover h2,
.panel-header:hover::after {
  color: var(--text);
  border-color: var(--text);
}

.panel-header:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: -2px;
}

.panel-body {
  padding: 0 16px 16px;
}

.segmented {
  display: flex;
  padding: 2px;
  margin-bottom: 12px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--bg);
}

.segmented button {
  flex: 1;
  appearance: none;
  padding: 5px 10px;
  border: 0;
  border-radius: 6px;
  background: transparent;
  color: var(--muted);
  font: inherit;
  cursor: pointer;
}

.segmented button.active {
  background: #3a3d44;
  color: var(--text);
}

.segmented button:disabled {
  opacity: 0.45;
  cursor: default;
}

.segmented-small button {
  padding: 3px 8px;
  font-size: 12px;
}

.status {
  margin: 0 0 6px;
}

.status.warn {
  color: #f0b45a;
}

.hint {
  margin: 0 0 12px;
  color: var(--muted);
  font-size: 12px;
  line-height: 1.45;
}

kbd {
  font: inherit;
  padding: 0 4px;
  border: 1px solid var(--border);
  border-radius: 4px;
  background: var(--bg);
}

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

.row .btn {
  flex: 1;
}

.check {
  display: flex;
  align-items: center;
  gap: 8px;
}

.slider {
  display: block;
  margin-bottom: 14px;
}

.slider-label {
  display: flex;
  justify-content: space-between;
  margin-bottom: 6px;
}

.slider-label output {
  color: var(--muted);
  font-variant-numeric: tabular-nums;
}

input[type='range'] {
  width: 100%;
  accent-color: var(--accent);
}

.sidebar-footer {
  border-top: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  gap: 8px;
  padding: 16px;
}

/* ---------- Buttons ---------- */

.btn {
  appearance: none;
  padding: 7px 14px;
  border: 1px solid var(--border);
  border-radius: 7px;
  background: #2d2f34;
  color: var(--text);
  font: inherit;
  cursor: pointer;
}

.btn:hover:not(:disabled) {
  background: #36383e;
}

.btn:disabled {
  opacity: 0.45;
  cursor: default;
}

.btn-primary {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
  font-weight: 500;
}

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

.btn.active {
  background: #3a3d44;
}

input:disabled + * {
  opacity: 0.5;
}

/* ---------- Film looks ---------- */

.field-label {
  margin-bottom: 6px;
}

.check-block {
  margin-bottom: 14px;
}

.check-spaced {
  margin-top: 6px;
}

/* Fixed heights so text appearing or wrapping never shifts the controls below. */
.auto-summary {
  margin: 6px 0 14px;
  height: calc(2 * 1.45em);
  overflow: hidden;
}

.looks {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 6px;
  margin-bottom: 8px;
}

.look {
  appearance: none;
  padding: 6px 8px;
  border: 1px solid var(--border);
  border-radius: 7px;
  background: #2d2f34;
  color: var(--text);
  font: inherit;
  font-size: 12px;
  text-align: left;
  cursor: pointer;
}

.look:hover:not(:disabled) {
  background: #36383e;
}

.look.active {
  border-color: var(--accent);
  background: rgba(79, 140, 255, 0.16);
}

.look:disabled {
  opacity: 0.45;
  cursor: default;
}

.look-description {
  height: calc(3 * 1.45em);
  overflow: hidden;
}

/* ---------- Manual colour ---------- */

.preset-bar,
.preset-name-row {
  display: flex;
  gap: 6px;
  margin-bottom: 12px;
}

.preset-bar select,
.preset-name-row input {
  flex: 1;
  min-width: 0;
  padding: 6px 8px;
  border: 1px solid var(--border);
  border-radius: 7px;
  background: var(--bg);
  color: var(--text);
  font: inherit;
}

.preset-bar select:disabled {
  opacity: 0.45;
}

.preset-name-row input:focus {
  outline: 2px solid var(--accent);
  outline-offset: -1px;
}

.btn-icon {
  padding: 6px 8px;
  line-height: 0;
}

.btn-icon svg {
  width: 14px;
  height: 14px;
  fill: currentColor;
}

.manual-group {
  margin: 4px 0 6px;
  font-size: 11px;
  color: var(--muted);
}

.manual-group:not(:first-child) {
  margin-top: 14px;
}

.slider-label[data-reset] {
  cursor: default;
}

.slider-label output.changed {
  color: var(--accent);
}

.btn-block {
  width: 100%;
  margin-top: 4px;
}

/* ---------- Crop & Rotate ---------- */

.crop-row .crop-aspect {
  flex: 1.4;
  min-width: 0;
  padding: 6px 8px;
  border: 1px solid var(--border);
  border-radius: 7px;
  background: var(--bg);
  color: var(--text);
  font: inherit;
}

.crop-aspect:disabled {
  opacity: 0.45;
}

.crop-row .btn-icon,
.geometry-buttons .btn-icon {
  flex: none;
}

.geometry-buttons .btn-icon {
  padding: 7px 9px;
}

.geometry-buttons {
  gap: 6px;
}

.geometry-buttons #geometry-reset {
  margin-left: auto;
  flex: none;
  padding-inline: 10px;
}

.stage.cropping .canvas {
  cursor: default;
}

/* ---------- Web ---------- */

/* Hints for a mouse and keyboard, or for touch. */
@media (pointer: coarse) {
  .fine-only {
    display: none !important;
  }
}
@media not (pointer: coarse) {
  .coarse-only {
    display: none !important;
  }
}

.tabs,
.sheet-handle {
  display: none;
}

.dropzone-note {
  margin: 14px 0 0;
  max-width: 240px;
  font-size: 12px;
  text-align: center;
}

.top-undo {
  display: none;
}

.icon-btn svg {
  display: block;
  width: 16px;
  height: 16px;
  fill: currentColor;
}

.compare svg {
  display: none;
  width: 16px;
  height: 16px;
  fill: currentColor;
}

.compare {
  -webkit-touch-callout: none;
  touch-action: none;
}

.toast {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 12px;
}

.toast-action {
  flex: none;
  padding: 5px 14px;
}

.unsupported {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 4px;
  min-height: 100dvh;
  padding: 32px max(24px, env(safe-area-inset-left));
  text-align: center;
}

.unsupported h1 {
  margin: 16px 0 8px;
  font-size: 20px;
}

.unsupported p {
  max-width: 380px;
  margin: 0 0 8px;
  color: var(--muted);
  font-size: 15px;
}

.unsupported img {
  border-radius: 22px;
}

.install-hint {
  position: fixed;
  z-index: 10;
  left: max(10px, env(safe-area-inset-left));
  right: max(10px, env(safe-area-inset-right));
  top: calc(env(safe-area-inset-top) + 62px);
  display: flex;
  align-items: flex-start;
  gap: 10px;
  padding: 10px 10px 10px 14px;
  background: rgba(35, 36, 40, 0.96);
  border: 1px solid var(--border);
  border-radius: 12px;
  box-shadow: 0 6px 24px rgba(0, 0, 0, 0.4);
}

.install-hint p {
  flex: 1;
  margin: 0;
}

/* ---------- Touch ---------- */

@media (pointer: coarse) {
  html,
  body {
    -webkit-tap-highlight-color: transparent;
    -webkit-text-size-adjust: 100%;
    touch-action: manipulation; /* no double-tap zoom on the controls */
  }

  /* Comfortable hit targets (about 44 pt). */
  .btn {
    min-height: 40px;
  }

  .btn-icon {
    min-width: 44px;
  }

  .segmented button {
    min-height: 36px;
  }

  .look {
    min-height: 40px;
  }

  .zoom-btn {
    height: 36px;
    min-width: 40px;
  }

  .check {
    min-height: 32px;
  }

  .check input {
    width: 20px;
    height: 20px;
  }

  input[type='range'] {
    height: 32px;
  }

  /* iOS zooms the page into text fields under 16 px. */
  select,
  input[type='text'] {
    font-size: 16px !important;
  }

  .slider-label[data-reset],
  .slider-label[data-reset-angle] {
    -webkit-touch-callout: none;
  }
}

/* ---------- Phone layout ---------- */
/* The photo fills the top; the five sections become tabs along the bottom,
   with the open one's controls in a sheet above them. Open sits top left,
   Compare and Export top right. */

@media (max-width: 760px) {
  html,
  body {
    font-size: 14px;
  }

  .app {
    /* minmax(0, …): the title column may shrink (its title ellipsizes) so a
       longer title or button label never makes the page wider than the screen. */
    grid-template-columns: minmax(0, 1fr) auto;
    grid-template-rows: auto minmax(0, 1fr) auto auto auto;
    grid-template-areas:
      'header footer'
      'viewport viewport'
      'handle handle'
      'panels panels'
      'tabs tabs';
  }

  .sidebar {
    display: contents;
  }

  .sidebar-header,
  .sidebar-footer {
    padding-top: calc(env(safe-area-inset-top) + 8px);
    padding-bottom: 8px;
    background: var(--panel);
    border-top: 0;
    border-bottom: 1px solid var(--border);
  }

  .sidebar-header {
    grid-area: header;
    justify-content: flex-start;
    gap: 10px;
    padding-left: max(12px, env(safe-area-inset-left));
    padding-right: 8px;
  }

  .sidebar-header h1 {
    font-size: 15px;
    order: 2;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }

  .sidebar-footer {
    grid-area: footer;
    flex-direction: row;
    align-items: center;
    gap: 8px;
    padding-left: 0;
    padding-right: max(12px, env(safe-area-inset-right));
  }

  .sidebar-footer .row {
    margin: 0;
  }

  .compare span {
    display: none;
  }

  .compare svg {
    display: block;
  }

  .compare,
  .icon-btn {
    padding-inline: 12px;
  }

  .top-undo {
    display: block;
  }

  .viewport {
    grid-area: viewport;
    padding: 8px max(8px, env(safe-area-inset-right)) 8px max(8px, env(safe-area-inset-left));
  }

  .sidebar-scroll {
    grid-area: panels;
    max-height: 36dvh;
    background: var(--panel);
    overscroll-behavior: contain;
  }

  /* The grab bar above the controls: swipe it down to hide them (the photo
     gets their space), up to bring them back. Tapping it does the same. */
  .sheet-handle {
    grid-area: handle;
    display: flex;
    justify-content: center;
    align-items: center;
    width: 100%;
    height: 22px;
    padding: 0;
    border: 0;
    border-top: 1px solid var(--border);
    background: var(--panel);
    cursor: pointer;
    touch-action: none;
    -webkit-tap-highlight-color: transparent;
  }

  .sheet-handle span {
    width: 36px;
    height: 5px;
    border-radius: 3px;
    background: var(--muted);
    opacity: 0.6;
  }

  .app.controls-hidden .sidebar-scroll {
    display: none;
  }

  .panel {
    display: none;
    border-bottom: 0;
  }

  .panel.tab-active {
    display: block;
  }

  .panel > .panel-header {
    display: none;
  }

  .panel-body {
    padding: 14px max(16px, env(safe-area-inset-right)) 8px max(16px, env(safe-area-inset-left));
  }

  .tabs {
    grid-area: tabs;
    display: flex;
    padding: 4px max(4px, env(safe-area-inset-right)) max(4px, env(safe-area-inset-bottom)) max(4px, env(safe-area-inset-left));
    background: var(--panel);
    border-top: 1px solid var(--border);
  }

  .tab {
    flex: 1;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 2px;
    min-height: 48px;
    padding: 6px 0 4px;
    border: 0;
    background: transparent;
    color: var(--muted);
    font: inherit;
    font-size: 11px;
    cursor: pointer;
  }

  .tab svg {
    width: 22px;
    height: 22px;
    fill: currentColor;
  }

  .tab[aria-selected='true'] {
    color: var(--accent);
  }

  .zoom-bar {
    top: 10px;
    bottom: auto;
    right: 10px;
  }

  .toast,
  .busy {
    bottom: 12px;
  }

  .dropzone-inner {
    padding: 36px 28px;
    border: 0;
  }

  .looks {
    grid-template-columns: 1fr 1fr 1fr;
  }

  .look-description {
    height: auto;
    min-height: calc(2 * 1.45em);
  }
}
