:root {
  --bg              : #f4f1ea;
  --ink             : #1f1b16;
  --panel           : #fffdf8;
  --line            : #d7cfc2;
  --accent          : #b24324;
  --muted           : #7a6f62;
  --image-median-rgb: 236, 228, 214;
  --paper-dark      : #d9cfbc;
  --paper-light     : #f7f2e7;
  --ink-soft        : #3f3328;
  --img-paper-grain : url("./images/paper-grain.png");
  --img-frame-wear  : url("./images/frame-wear.png");
  --img-dust-specks : url("./images/dust-specks.png");
  --img-vignette    : url("./images/vignette-soft.png");
}

* {
  box-sizing: border-box;
}

[hidden] {
  display: none !important;
}

body {
  margin     : 0;
  font-family: "Cormorant Garamond", Georgia, serif;
  color      : var(--ink);
  background : radial-gradient(circle at 18% 12%, rgba(255, 251, 240, 0.78), rgba(255, 251, 240, 0) 42%),
               radial-gradient(circle at 84% 80%, rgba(90, 64, 35, 0.1), rgba(90, 64, 35, 0) 38%),
               linear-gradient(132deg, rgba(var(--image-median-rgb), 0.22), rgba(var(--image-median-rgb), 0.58));
  transition : background 500ms ease;
  position   : relative;
}

body::before {
  content         : "";
  position        : fixed;
  inset           : 0;
  pointer-events  : none;
  opacity         : 0.2;
  mix-blend-mode  : multiply;
  background-image: var(--img-paper-grain),
    repeating-linear-gradient(0deg,
      rgba(53, 40, 30, 0.05) 0,
      rgba(53, 40, 30, 0.05) 1px,
      rgba(255, 255, 255, 0) 1px,
      rgba(255, 255, 255, 0) 3px),
    repeating-linear-gradient(90deg,
      rgba(53, 40, 30, 0.028) 0,
      rgba(53, 40, 30, 0.028) 1px,
      rgba(255, 255, 255, 0) 1px,
      rgba(255, 255, 255, 0) 4px);
  background-size : 260px 260px, auto, auto;
}

.topbar {
  padding      : 1rem 1.25rem;
  border-bottom: 1px solid var(--line);
  background   : linear-gradient(180deg, rgba(255, 252, 245, 0.95), rgba(242, 235, 223, 0.95));
  box-shadow   : inset 0 -8px 18px rgba(122, 95, 66, 0.08);
}

.topbar-main {
  display        : flex;
  align-items    : flex-start;
  justify-content: space-between;
  gap            : 1rem;
}

.topbar h1 {
  margin   : 0;
  font-size: 1.5rem;
  letter-spacing: 0.03em;
}

.topbar p {
  margin: 0.35rem 0 0;
  color : var(--muted);
}

.topbar-links {
  display      : inline-flex;
  flex-wrap    : wrap;
  gap          : 0.45rem;
  margin-top   : 0.1rem;
  align-self   : flex-start;
  justify-content: flex-end;
}

.topbar-links a {
  color          : #6a2815;
  text-decoration: none;
  border         : 1px solid rgba(122, 95, 66, 0.35);
  border-radius  : 999px;
  padding        : 0.25rem 0.6rem;
  background     : rgba(255, 255, 255, 0.55);
  font-weight    : 600;
}

.topbar-links a:hover,
.topbar-links a:focus-visible {
  background: rgba(255, 255, 255, 0.8);
}

.layout {
  display              : grid;
  gap                  : 1rem;
  padding              : 1rem;
  grid-template-columns: repeat(12, 1fr);
  min-height           : calc(100vh - 76px);
}

.panel {
  border       : 1px solid var(--line);
  border-radius: 10px;
  background   : linear-gradient(165deg, rgba(255, 252, 244, 0.96), rgba(243, 234, 219, 0.96));
  padding      : 0.9rem;
  position     : relative;
  box-shadow   : 0 12px 24px rgba(50, 34, 20, 0.12), inset 0 0 0 1px rgba(255, 255, 255, 0.65);
}

.panel::before {
  content       : "";
  position      : absolute;
  inset         : -5px;
  border        : 1px solid rgba(96, 70, 44, 0.35);
  border-radius : 12px;
  transform     : rotate(var(--frame-tilt, 0deg));
  pointer-events: none;
  opacity       : 0.68;
  box-shadow    : inset 0 0 16px rgba(108, 80, 50, 0.12);
  background-image: var(--img-frame-wear);
  background-size : 100% 100%;
  background-repeat: no-repeat;
  background-position: center;
  mix-blend-mode  : soft-light;
}

.panel::after {
  content         : "";
  position        : absolute;
  inset           : -6px;
  pointer-events  : none;
  background-image: var(--img-dust-specks);
  background-size : 360px 360px;
  background-repeat: repeat;
  background-position: center;
  opacity         : 0.08;
  mix-blend-mode  : soft-light;
  border-radius   : 12px;
}

.input-panel {
  --frame-tilt: -0.65deg;
}

.source-panel {
  --frame-tilt: 0.5deg;
}

.overlay-panel {
  --frame-tilt: -0.35deg;
}

.export-panel {
  --frame-tilt: 0deg;
  background  : rgba(255, 252, 246, 0.98);
  box-shadow  : 0 8px 16px rgba(50, 34, 20, 0.08), inset 0 0 0 1px rgba(255, 255, 255, 0.72);
}

.export-panel::before,
.export-panel::after {
  content: none;
}

.input-panel {
  grid-column: 1 / span 6;
  grid-row   : 1;
}

.overlay-panel {
  grid-column: 7 / span 6;
  grid-row   : 1;
}

.source-panel {
  grid-column: 1 / span 7;
  grid-row   : 2;
}

.export-panel {
  grid-column: 1 / span 12;
  grid-row   : 3;
}

body.app-ready .source-panel {
  grid-column: 1 / span 6;
  grid-row   : 1;
}

body.app-ready .overlay-panel {
  grid-column: 7 / span 6;
  grid-row   : 1;
}

body.app-ready .export-panel {
  grid-column: 1 / span 12;
  grid-row   : 2;
}

.input-panel {
  transition      : transform 520ms cubic-bezier(0.19, 0.89, 0.29, 1.03), opacity 420ms ease, max-height 420ms ease, padding 420ms ease, margin 420ms ease, border-width 420ms ease;
  transform-origin: center top;
  max-height      : 1200px;
}

body:not(.app-ready) .input-panel {
  transform: translate(18vw, 24vh);
}

.input-panel.is-collapsed {
  transform     : translate(0, 0) scale(0.92);
  opacity       : 0;
  max-height    : 0;
  padding-top   : 0;
  padding-bottom: 0;
  margin        : 0;
  border-width  : 0;
  overflow      : hidden;
  pointer-events: none;
  visibility    : hidden;
}

.panel-enter {
  opacity  : 0;
  transform: translateY(18px);
}

.panel-enter.is-visible {
  animation      : panelFadeIn 460ms ease forwards;
  animation-delay: var(--enter-delay, 0ms);
}

h2 {
  margin   : 0 0 0.7rem;
  font-size: 1.2rem;
  letter-spacing: 0.02em;
  color    : var(--ink-soft);
}

label {
  display  : block;
  font-size: 0.9rem;
  margin   : 0.35rem 0;
}

input[type="text"],
textarea {
  width        : 100%;
  border       : 1px solid var(--line);
  border-radius: 6px;
  padding      : 0.55rem;
  font         : inherit;
}

textarea {
  font-family: "IBM Plex Mono", "Fira Code", "Cascadia Code", Consolas, "Courier New", monospace;
  line-height: 1.4;
}

button {
  border       : 1px solid var(--accent);
  background   : linear-gradient(180deg, #9c3f24, #7c2e18);
  color        : #fff;
  border-radius: 6px;
  padding      : 0.5rem 0.7rem;
  cursor       : pointer;
  font-family  : "Cormorant Garamond", Georgia, serif;
  font-weight  : 600;
  letter-spacing: 0.02em;
}

.row {
  display    : flex;
  gap        : 0.5rem;
  align-items: center;
  margin-top : 0.5rem;
}

.row.wrap {
  flex-wrap: wrap;
}

.export-action-group {
  margin-top: 0.55rem;
}

.export-action-group-save {
  margin-top   : 0.7rem;
  padding-top  : 0.65rem;
  border-top   : 1px dashed rgba(122, 95, 66, 0.35);
}

.export-action-label {
  margin        : 0 0 0.35rem;
  font-size     : 0.9rem;
  letter-spacing: 0.02em;
  color         : var(--ink-soft);
  font-weight   : 600;
}

.export-actions-save button {
  background  : linear-gradient(180deg, #7f2711, #651b0b);
  border-color: #651b0b;
}

.viewer-links-row {
  margin     : 0.65rem 0 0.45rem;
  padding    : 0.45rem 0.6rem;
  border     : 1px solid rgba(122, 95, 66, 0.28);
  border-radius: 6px;
  background : rgba(255, 255, 255, 0.6);
  color      : var(--ink-soft);
  font-size  : 0.98rem;
}

.viewer-links-row a {
  color          : #7c2e18;
  font-weight    : 700;
  text-decoration: underline;
  margin-left    : 0.25rem;
}

.viewer-links-separator {
  margin: 0 0.2rem 0 0.35rem;
  color : rgba(91, 67, 45, 0.55);
}

.status {
  color    : var(--muted);
  font-size: 0.85rem;
}

.load-spinner {
  display     : inline-block;
  width       : 1rem;
  height      : 1rem;
  border      : 2px solid var(--line);
  border-top  : 2px solid var(--accent);
  border-radius: 50%;
  animation   : spin 0.7s linear infinite;
  vertical-align: middle;
  margin-left : 0.4rem;
}

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

.hint {
  margin-top: 0;
  color     : var(--muted);
  font-size : 0.85rem;
}

.source-preview,
.overlay-preview {
  position  : relative;
  min-height: 320px;
  background: linear-gradient(145deg, var(--paper-light), var(--paper-dark));
  border    : 1px solid rgba(120, 95, 67, 0.45);
  overflow  : hidden;
  box-shadow: inset 0 0 0 1px rgba(255, 255, 255, 0.45), inset 0 0 24px rgba(68, 48, 28, 0.18);
}

.source-preview img {
  position  : absolute;
  inset     : 0;
  width     : 100%;
  height    : 100%;
  object-fit: contain;
}

.overlay-preview {
  display: block;
  padding: 1rem;
}

.crop-box {
  position           : absolute;
  left               : 50%;
  top                : 50%;
  border             : 2px solid;
  width              : clamp(140px, 24vw, 220px);
  aspect-ratio       : 1;
  display            : grid;
  place-items        : center;
  font-weight        : 700;
  font-size          : 1.1rem;
  letter-spacing     : 0.08em;
  text-transform     : uppercase;
  background         : rgba(255, 255, 255, 0.12);
  -webkit-user-select: none;
  user-select        : none;
  border-radius      : 10px;
  box-shadow         : 0 0 0 1px rgba(255, 255, 255, 0.22) inset, 0 12px 24px rgba(0, 0, 0, 0.12);
  transform          : translate(-50%, -50%);
  touch-action       : none;
  cursor             : move;
}

.crop-box.left {
  border-color: #d63a3a;
  background  : linear-gradient(135deg, rgba(214, 58, 58, 0.28), rgba(214, 58, 58, 0.08));
  color       : #8b1e1e;
  box-shadow  : 0 0 0 1px rgba(214, 58, 58, 0.25) inset, 0 0 22px rgba(214, 58, 58, 0.24), 0 12px 24px rgba(0, 0, 0, 0.12);
  transform   : translate(calc(-50% - clamp(80px, 11vw, 140px)), -50%);
}

.crop-box.right {
  border-color: #28b7c8;
  background  : linear-gradient(135deg, rgba(40, 183, 200, 0.3), rgba(40, 183, 200, 0.08));
  color       : #0d6b77;
  box-shadow  : 0 0 0 1px rgba(40, 183, 200, 0.25) inset, 0 0 22px rgba(40, 183, 200, 0.24), 0 12px 24px rgba(0, 0, 0, 0.12);
  transform   : translate(calc(-50% + clamp(80px, 11vw, 140px)), -50%);
}

.resize-handle {
  position     : absolute;
  right        : -5px;
  bottom       : -5px;
  width        : 18px;
  height       : 18px;
  border-radius: 50%;
  border       : 2px solid currentColor;
  background   : rgba(255, 255, 255, 0.86);
  box-shadow   : 0 2px 6px rgba(0, 0, 0, 0.16);
  cursor       : nwse-resize;
}

.resize-handle-top-left {
  left  : -5px;
  top   : -5px;
  right : auto;
  bottom: auto;
}

.crop-preview {
  position         : relative;
  width            : clamp(140px, 22vw, 240px);
  aspect-ratio     : 1;
  border           : 2px solid;
  border-radius    : 12px;
  overflow         : hidden;
  background-color : #ddd5c8;
  background-repeat: no-repeat;
  box-shadow       : 0 12px 24px rgba(0, 0, 0, 0.12);
  display          : grid;
  place-items      : center;
  transition       : filter 180ms ease;
}

.overlay-preview .crop-preview {
  position  : absolute;
  inset     : 0;
  margin    : auto;
  transition: background-color 320ms ease, background-blend-mode 320ms ease;
}

.overlay-preview.anaglyph-mode .crop-preview {
  background-color     : #28b7c8;
  background-blend-mode: overlay;
}

.overlay-preview.anaglyph-mode .crop-preview.left {
  background-color: #d63a3a;
}

.overlay-preview.full-color .crop-preview {
  background-color: transparent;
}

.crop-preview span {
  position      : relative;
  z-index       : 1;
  font-weight   : 700;
  font-size     : 1.4rem;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color         : rgba(31, 27, 22, 0.82);
  text-shadow   : 0 1px 0 rgba(255, 255, 255, 0.5);
}

.crop-preview.left {
  border-color: #d63a3a;
  box-shadow  : 0 0 0 1px rgba(214, 58, 58, 0.2) inset, 0 0 22px rgba(214, 58, 58, 0.18), 0 12px 24px rgba(0, 0, 0, 0.12);
}

.crop-preview.right {
  border-color: #28b7c8;
  box-shadow  : 0 0 0 1px rgba(40, 183, 200, 0.2) inset, 0 0 22px rgba(40, 183, 200, 0.18), 0 12px 24px rgba(0, 0, 0, 0.12);
}

.control-grid {
  display   : grid;
  gap       : 0.55rem;
  margin-top: 0.55rem;
}

.control-row {
  display              : grid;
  grid-template-columns: minmax(105px, 150px) 1fr minmax(40px, 56px);
  gap                  : 0.6rem;
  align-items          : center;
}

.control-row input[type="range"] {
  width: 100%;
}

.control-row output {
  text-align          : right;
  font-variant-numeric: tabular-nums;
  color               : var(--muted);
}

.inline-toggle {
  display    : inline-flex;
  align-items: center;
  gap        : 0.45rem;
}

.overlay-actions {
  justify-content: center;
}

.preview-control-grid {
  margin-top: 0.8rem;
}

#blinkToggleBtn[aria-pressed="true"] {
  background  : #7f2711;
  border-color: #7f2711;
}

textarea,
input[type="text"] {
  background: rgba(255, 253, 247, 0.88);
}

@keyframes panelFadeIn {
  from {
    opacity  : 0;
    transform: translateY(18px);
  }

  to {
    opacity  : 1;
    transform: translateY(0);
  }
}

@media (max-width: 840px) {

  .topbar-main {
    flex-direction: column;
    align-items   : flex-start;
  }

  .topbar-links {
    justify-content: flex-start;
  }

  .input-panel,
  .source-panel,
  .overlay-panel,
  .export-panel {
    grid-column: span 12;
    grid-row   : auto;
  }

  body:not(.app-ready) .input-panel {
    transform: translate(0, 14vh);
  }
}
