:root {
  --bg: #111317;
  --card: #1a1d23;
  --card-2: #22262e;
  --line: #2e333d;
  --text: #eef0f3;
  --text-2: #a4abb8;
  --text-3: #6f7785;
  --accent: #f5b83d;
  --sharp: #4ade80;
  --focus: #f05252;
  --radius: 14px;
  --font: "Vazirmatn", Tahoma, -apple-system, "Segoe UI", sans-serif;
}

* { box-sizing: border-box; }

html, body { margin: 0; }

body {
  background: var(--bg);
  color: var(--text);
  font-family: var(--font);
  font-size: 15px;
  line-height: 1.7;
  -webkit-font-smoothing: antialiased;
  padding: 16px clamp(16px, 3vw, 32px) 32px;
}

h1, h2, p { margin: 0; }

.top {
  max-width: 1400px;
  margin: 0 auto 14px;
}

.top h1 {
  font-size: 1.45rem;
  font-weight: 800;
}

.top p {
  color: var(--text-2);
}

/* ---------- Layout ---------- */
.layout {
  max-width: 1400px;
  margin: 0 auto;
  display: grid;
  grid-template-columns: minmax(0, 1fr) 360px;
  grid-template-areas:
    "stage panel"
    "diagram panel";
  grid-template-rows: auto 1fr;
  gap: 16px;
  align-items: start;
}

.stage { grid-area: stage; }
.panel { grid-area: panel; display: flex; flex-direction: column; gap: 12px; align-self: start; }
.diagram-card { grid-area: diagram; }

@media (max-width: 960px) {
  .layout {
    grid-template-columns: minmax(0, 1fr);
    grid-template-areas: "stage" "panel" "diagram";
    grid-template-rows: auto;
  }

  /* Keep the picture and the lens in view while scrolling through the lesson */
  .stage {
    position: sticky;
    top: 0;
    z-index: 10;
    padding-block: 8px;
    margin-block: -8px;
    background: var(--bg);
  }

  .stage .barrel-card { margin-top: 8px; padding: 8px; }
  .stage .barrel-head { display: none; }

  .vf { max-width: none; }
}

.card {
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 14px 16px;
}

.card h2 {
  font-size: 1rem;
  font-weight: 800;
}

/* ---------- Viewfinder ---------- */
.vf {
  position: relative;
  width: 100%;
  /* leave room for the lens barrel under the picture on desktop screens */
  max-width: max(420px, calc((100vh - 400px) * 1.5));
  margin-inline: auto;
  aspect-ratio: 3 / 2;
  overflow: hidden;
  border-radius: var(--radius);
  background: #cfe3f5;
  cursor: crosshair;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.45);
  touch-action: manipulation;
  user-select: none;
  -webkit-user-select: none;
}

.vf canvas.layer,
.vf .peak-band {
  position: absolute;
  left: 0;
  display: block;
  pointer-events: none;
}

.vf .peak-band {
  width: 100%;
  background: rgba(74, 222, 128, 0.22);
  border-top: 2px solid rgba(74, 222, 128, 0.9);
  border-bottom: 2px solid rgba(74, 222, 128, 0.9);
  mix-blend-mode: normal;
}

.vf-hint {
  position: absolute;
  top: 12px;
  left: 50%;
  transform: translateX(-50%);
  z-index: 50;
  max-width: calc(100% - 24px);
  padding: 6px 14px;
  border-radius: 999px;
  background: rgba(17, 19, 23, 0.78);
  color: #fff;
  font-size: 0.85rem;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  pointer-events: none;
  transition: opacity 0.4s ease;
}

.vf-hint.gone { opacity: 0; }

.vf-reticle {
  position: absolute;
  z-index: 40;
  width: 56px;
  height: 56px;
  margin: -28px 0 0 -28px;
  border: 2px solid #fff;
  border-radius: 6px;
  box-shadow: 0 0 0 1px rgba(0, 0, 0, 0.35);
  pointer-events: none;
  animation: reticle 0.9s ease forwards;
}

.vf-reticle[hidden] { display: none; }

@keyframes reticle {
  0% { transform: scale(1.5); opacity: 0; border-color: #fff; }
  25% { transform: scale(1); opacity: 1; border-color: #fff; }
  55% { border-color: var(--sharp); }
  100% { transform: scale(1); opacity: 0; border-color: var(--sharp); }
}

.vf-hud {
  position: absolute;
  z-index: 45;
  bottom: 10px;
  inset-inline-start: 10px;
  display: flex;
  gap: 6px;
  pointer-events: none;
}

.hud-item {
  padding: 3px 10px;
  border-radius: 8px;
  background: rgba(17, 19, 23, 0.75);
  color: #fff;
  font-size: 0.85rem;
}

.hud-item bdi { font-weight: 800; color: var(--accent); }

/* ---------- Result ---------- */
.result {
  border-color: rgba(74, 222, 128, 0.35);
  background: linear-gradient(180deg, rgba(74, 222, 128, 0.09), rgba(74, 222, 128, 0.02)), var(--card);
}

.result-label {
  color: var(--text-2);
  font-size: 0.85rem;
}

.result-main {
  font-size: 1.45rem;
  font-weight: 800;
  color: var(--sharp);
  line-height: 1.5;
}

.result-sub {
  color: var(--text);
  font-weight: 600;
}

.result-tip {
  margin-top: 6px;
  color: var(--text-2);
  font-size: 0.88rem;
}

.result-tip:empty { display: none; }

/* ---------- Controls ---------- */
.control-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 10px;
}

.control-value {
  font-weight: 800;
  color: var(--accent);
  font-size: 1.05rem;
}

.iris {
  width: 36px;
  height: 36px;
}

.iris-ring { fill: #0b0c0f; stroke: #4a505c; stroke-width: 1.5; }
.iris-hole { fill: #fff3cf; transition: none; }

.stops {
  direction: ltr;
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 6px;
}

.stop-btn {
  height: 38px;
  border-radius: 9px;
  border: 1px solid var(--line);
  background: var(--card-2);
  color: var(--text);
  font: 700 0.9rem/1 ui-monospace, "SF Mono", Menlo, monospace;
  cursor: pointer;
  transition: background 0.15s, border-color 0.15s, color 0.15s;
}

.stop-btn:hover { border-color: #4a505c; }

.stop-btn[aria-pressed="true"] {
  background: var(--accent);
  border-color: var(--accent);
  color: #1a1206;
}

.scale-caption {
  display: flex;
  justify-content: space-between;
  gap: 8px;
  margin-top: 8px;
  color: var(--text-3);
  font-size: 0.78rem;
  line-height: 1.5;
}

.slider-row {
  position: relative;
  padding-bottom: 20px;
}

.slider-row input[type="range"] {
  width: 100%;
  margin: 0;
  accent-color: var(--accent);
  height: 28px;
  cursor: pointer;
}

.slider-ticks {
  position: absolute;
  left: 9px;
  right: 9px;
  bottom: 0;
  height: 18px;
  font-size: 0.75rem;
  color: var(--text-3);
}

.slider-ticks span {
  position: absolute;
  transform: translateX(-50%);
}

/* ---------- Toggle ---------- */
.toggle {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 4px 4px;
  cursor: pointer;
  color: var(--text-2);
}

.toggle input {
  position: absolute;
  opacity: 0;
  pointer-events: none;
}

.toggle-track {
  flex: none;
  width: 42px;
  height: 24px;
  border-radius: 999px;
  background: var(--card-2);
  border: 1px solid var(--line);
  position: relative;
  transition: background 0.2s;
}

.toggle-thumb {
  position: absolute;
  top: 2px;
  inset-inline-start: 2px;
  width: 18px;
  height: 18px;
  border-radius: 50%;
  background: var(--text-2);
  transition: inset-inline-start 0.2s, background 0.2s;
}

.toggle input:checked + .toggle-track { background: rgba(74, 222, 128, 0.25); border-color: var(--sharp); }
.toggle input:checked + .toggle-track .toggle-thumb { inset-inline-start: 20px; background: var(--sharp); }
.toggle input:focus-visible + .toggle-track { outline: 2px solid var(--accent); outline-offset: 2px; }

/* ---------- Presets ---------- */
.presets h2 { margin-bottom: 8px; }

.preset-list {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 6px;
}

.preset-btn {
  text-align: start;
  padding: 8px 10px;
  border-radius: 10px;
  border: 1px solid var(--line);
  background: var(--card-2);
  color: var(--text);
  font: 600 0.85rem/1.5 var(--font);
  cursor: pointer;
}

.preset-btn small {
  display: block;
  color: var(--text-3);
  font-weight: 400;
  font-size: 0.75rem;
}

.preset-btn:hover { border-color: #4a505c; }
.preset-btn[aria-pressed="true"] { border-color: var(--accent); }

.preset-note {
  margin-top: 8px;
  color: var(--text-2);
  font-size: 0.88rem;
}

.preset-note:empty { display: none; }

/* ---------- Diagram ---------- */
.diagram-head {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 6px 16px;
  margin-bottom: 4px;
}

.legend {
  display: flex;
  flex-wrap: wrap;
  gap: 4px 14px;
  font-size: 0.8rem;
  color: var(--text-2);
}

.lg {
  display: inline-block;
  width: 14px;
  height: 10px;
  margin-inline-end: 6px;
  border-radius: 3px;
  vertical-align: middle;
}

.lg-curve { background: rgba(245, 184, 61, 0.55); }
.lg-zone { background: rgba(74, 222, 128, 0.55); }
.lg-focus { background: var(--focus); width: 4px; }

#diagram {
  display: block;
  width: 100%;
  height: auto;
  cursor: ew-resize;
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
}

.diagram-note {
  color: var(--text-3);
  font-size: 0.8rem;
}

button:focus-visible,
input[type="range"]:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

.unit { color: var(--text-3); font-weight: 400; font-size: 0.8rem; }

.ltr {
  direction: ltr;
  unicode-bidi: isolate;
  font-family: ui-monospace, "SF Mono", Menlo, monospace;
  font-size: 0.92em;
}

/* ---------- Lens barrel (top view) ---------- */
.barrel-card {
  margin-top: 12px;
  padding: 12px 14px 14px;
}

.barrel-head {
  display: flex;
  flex-wrap: wrap;
  align-items: baseline;
  justify-content: space-between;
  gap: 4px 12px;
  margin-bottom: 8px;
}

.barrel-hint {
  color: var(--text-3);
  font-size: 0.8rem;
}

.barrel-wrap {
  display: flex;
  gap: 10px;
  align-items: stretch;
}

.barrel-labels {
  flex: none;
  width: 118px;
  margin: 0;
  padding: 0;
  list-style: none;
  position: relative;
  height: 238px;
  font-size: 0.78rem;
  line-height: 1.35;
  color: var(--text-2);
}

.barrel-labels li {
  position: absolute;
  inset-inline: 0;
  display: flex;
  flex-direction: column;
  justify-content: center;
  padding-inline-start: 8px;
  border-inline-start: 3px solid var(--line);
}

.barrel-labels small { color: var(--text-3); font-size: 0.7rem; }

/* match the SVG band positions (see Y in barrel.js) */
.bl-grip { top: 34px; height: 34px; }
.bl-dist { top: 68px; height: 72px; }
.bl-dof { top: 140px; height: 54px; border-color: #7a818e !important; color: var(--text) !important; font-weight: 600; }
.bl-ap { top: 194px; height: 42px; }

.barrel {
  flex: 1;
  min-width: 0;
  display: block;
  width: 100%;
  border-radius: 10px;
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
  font-family: var(--font);
}

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

.barrel .b-move { cursor: ew-resize; }
.barrel.dragging, .barrel.dragging .b-move { cursor: grabbing; }
.barrel .b-fixed { cursor: not-allowed; }

.barrel .b-ridge { fill: #1d2027; }
.barrel .b-tick { stroke: #e9ebef; stroke-width: 1.4; }
.barrel .b-tick-fine { stroke: #8c93a0; stroke-width: 1; }
.barrel .b-tick-ft { stroke: #f2a93b; stroke-width: 1.2; }

.barrel .b-m {
  fill: #f4f5f7;
  font: 700 15px ui-monospace, "SF Mono", Menlo, monospace;
  text-anchor: middle;
}

.barrel .b-ft {
  fill: #f2a93b;
  font: 600 12px ui-monospace, "SF Mono", Menlo, monospace;
  text-anchor: middle;
}

.barrel .b-dof { opacity: 0.4; transition: opacity 0.2s; }
.barrel .b-dof.on { opacity: 1; }
.barrel .b-dof-line { stroke: var(--c); stroke-width: 1.6; }
.barrel .b-dof.on .b-dof-line { stroke-width: 2.6; }
.barrel .b-dof-num {
  fill: var(--c);
  font: 700 13px ui-monospace, "SF Mono", Menlo, monospace;
  text-anchor: middle;
  paint-order: stroke;
  stroke: #3b414c;
  stroke-width: 3px;
}
.barrel .b-dof.on .b-dof-num { font-size: 15px; }
.barrel .b-dof.crowded .b-dof-num { opacity: 0; }

.barrel .b-ap-num {
  fill: var(--c);
  opacity: 0.55;
  font: 700 16px ui-monospace, "SF Mono", Menlo, monospace;
  text-anchor: middle;
  cursor: pointer;
}
.barrel .b-ap-num.on { opacity: 1; font-size: 19px; }

.barrel .b-zone { fill: rgba(74, 222, 128, 0.2); pointer-events: none; }
.barrel .b-guide { stroke-width: 2; stroke-dasharray: 5 3; pointer-events: none; }

.barrel .b-tag rect { fill: #14171c; stroke-width: 1.5; }
.barrel .b-tag text {
  fill: #fff;
  font: 700 13px var(--font);
  text-anchor: middle;
  direction: rtl;
}
.barrel .b-tag { pointer-events: none; }

.barrel .b-hl {
  fill: none;
  stroke: var(--accent);
  stroke-width: 2.5;
  pointer-events: none;
  animation: hl-pulse 1.6s ease-in-out infinite;
}

.barrel .b-flash-text {
  fill: #fff;
  font: 600 13px var(--font);
  text-anchor: middle;
  direction: rtl;
}

@keyframes hl-pulse {
  0%, 100% { stroke-opacity: 1; }
  50% { stroke-opacity: 0.35; }
}

@media (max-width: 640px) {
  .barrel-labels { display: none; }
}

/* ---------- Lesson ---------- */
.lesson {
  border-color: rgba(245, 184, 61, 0.35);
}

.lesson-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  margin-bottom: 6px;
}

.lesson-count {
  color: var(--accent);
  font-size: 0.8rem;
  font-weight: 700;
}

.lesson-dots {
  display: flex;
  gap: 5px;
}

.lesson-dots .dot {
  width: 9px;
  height: 9px;
  padding: 0;
  border: none;
  border-radius: 50%;
  background: var(--line);
  cursor: pointer;
  position: relative;
}

.lesson-dots .dot::after { content: ""; position: absolute; inset: -6px; }
.lesson-dots .dot.on { background: var(--accent); }

.lesson-title {
  margin: 0 0 6px;
  font-size: 1.05rem;
  font-weight: 800;
}

.lesson-body {
  color: #d3d7de;
  font-size: 0.9rem;
  line-height: 1.85;
}

.lesson-body p { margin: 0 0 8px; }
.lesson-body ul { margin: 0 0 8px; padding-inline-start: 1.1rem; }
.lesson-body b { color: #fff; }

.lesson-body .rule {
  padding: 6px 10px;
  border-radius: 8px;
  background: rgba(245, 184, 61, 0.1);
  border: 1px solid rgba(245, 184, 61, 0.3);
  color: #fff;
  font-weight: 700;
}

.lesson-nav {
  display: flex;
  gap: 6px;
  margin-top: 6px;
}

.btn {
  height: 36px;
  padding: 0 14px;
  border-radius: 9px;
  border: 1px solid var(--line);
  background: var(--card-2);
  color: var(--text);
  font: 700 0.85rem var(--font);
  cursor: pointer;
}

.btn:disabled { opacity: 0.4; cursor: default; }
.btn-primary { margin-inline-start: auto; background: var(--accent); border-color: var(--accent); color: #1a1206; }
.btn-ghost { background: transparent; }

.diagram-card.pulse {
  animation: card-pulse 1.3s ease-in-out 2;
}

@keyframes card-pulse {
  0%, 100% { border-color: var(--line); box-shadow: none; }
  50% { border-color: var(--accent); box-shadow: 0 0 0 3px rgba(245, 184, 61, 0.2); }
}
