:root {
  --blue-900: #2f4a63;
  --blue-700: #3d5f80;
  --blue-500: #5b7fa3;
  --blue-300: #8fa9c0;
  --blue-100: #dbe6ee;
  --blue-050: #f2f6f9;
  --orange: #e08a4f;
  --orange-dark: #c76f36;
  --ink: #33404a;
  --ink-soft: #64727c;
  --line: #dde3e8;
  --bg: #f7f9fb;
  --card-bg: #ffffff;
  --calc-bg: #fdfaf5;
  --radius: 14px;
  --shadow: 0 1px 2px rgba(30, 45, 60, .06), 0 6px 20px rgba(30, 45, 60, .06);
  font-size: 16px;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--ink);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  -webkit-tap-highlight-color: transparent;
}

.app {
  max-width: 1100px;
  margin: 0 auto;
  padding: 16px 16px calc(48px + env(safe-area-inset-bottom));
}

/* ===== Top bar ===== */
.topbar {
  display: grid;
  grid-template-columns: 56px 1fr;
  gap: 12px 16px;
  align-items: center;
  margin-bottom: 20px;
}

.camera-thumb {
  width: 56px;
  height: 56px;
  border-radius: 12px;
  background: var(--blue-050);
  border: 1px solid var(--line);
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
  grid-row: 1 / span 2;
}
.camera-thumb img { width: 100%; height: 100%; object-fit: cover; }
.camera-icon { width: 34px; height: 34px; }

.camera-select {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.select-field {
  position: relative;
}
.select-field input {
  width: 100%;
  height: 42px;
  padding: 0 40px 0 14px;
  border-radius: 10px;
  border: 1px solid var(--line);
  background: #fff;
  font-size: 15px;
  color: var(--ink);
}
.select-field input:focus {
  outline: none;
  border-color: var(--blue-500);
  box-shadow: 0 0 0 3px var(--blue-100);
}
.select-field input::placeholder { color: #9aa7b0; }
.icon-search {
  position: absolute;
  right: 12px;
  top: 50%;
  transform: translateY(-50%);
  width: 18px;
  height: 18px;
  color: #9aa7b0;
  pointer-events: none;
}

.btn-add {
  grid-column: 1 / -1;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  height: 42px;
  border-radius: 10px;
  border: 1px dashed var(--blue-300);
  background: var(--blue-050);
  color: var(--blue-700);
  font-size: 14.5px;
  font-weight: 600;
  cursor: pointer;
}
.btn-add:hover { background: var(--blue-100); }
.icon-plus { width: 18px; height: 18px; }

@media (min-width: 640px) {
  .topbar {
    grid-template-columns: 56px 1fr auto;
  }
  .camera-select {
    flex-direction: row;
  }
  .btn-add {
    grid-column: auto;
    grid-row: 1 / span 2;
    align-self: center;
    padding: 0 18px;
    white-space: nowrap;
  }
}

/* ===== Layout ===== */
.content {
  display: grid;
  gap: 20px;
}

@media (min-width: 900px) {
  .content {
    grid-template-columns: 1.6fr 1fr;
    align-items: start;
  }
}

.card {
  background: var(--card-bg);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 20px;
}

.card-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 4px;
}
.card-head h1, .card-head h2 {
  font-size: 19px;
  font-weight: 700;
  margin: 0;
  color: var(--blue-900);
}

.card-actions {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
}

.card-actions .btn-sm {
  height: 32px;
  padding: 0 14px;
  font-size: 12.5px;
}

.btn-reset {
  border: 1px solid var(--orange);
  color: var(--orange-dark);
  background: #fff;
  border-radius: 999px;
  padding: 6px 18px;
  font-size: 13.5px;
  font-weight: 600;
  cursor: pointer;
}
.btn-reset:hover { background: #fdf0e6; }
.btn-reset:active { transform: translateY(1px); }

.hint {
  color: var(--ink-soft);
  font-size: 13.5px;
  margin: 10px 0 6px;
  line-height: 1.5;
}
.hint-combos {
  background: var(--blue-050);
  border: 1px solid var(--blue-100);
  border-radius: 10px;
  padding: 10px 14px;
  margin-bottom: 16px;
}
.hint-label {
  font-size: 12.5px;
  font-weight: 700;
  color: var(--blue-700);
  text-transform: uppercase;
  letter-spacing: .03em;
}
.hint-combos ul {
  margin: 6px 0 0;
  padding-left: 18px;
  font-size: 13.5px;
  color: var(--ink-soft);
  line-height: 1.6;
}

/* ===== Diagram ===== */
.diagram-wrap {
  background: var(--calc-bg);
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 8px;
  margin-bottom: 20px;
}
.diagram { width: 100%; height: auto; display: block; }

.cone { fill: #fbeedd; opacity: .8; }
.plane { fill: var(--blue-700); opacity: .9; }
.centerline { stroke: #c9bda8; stroke-dasharray: 6 6; stroke-width: 2; }
.cam-body { fill: #dfe7ee; stroke: #b9c7d3; stroke-width: 2; }
.cam-detail { fill: #b9c7d3; }
.lens-body { fill: var(--blue-500); }
.lens-ring { fill: #fff; opacity: .85; }
.lens-glass { fill: #2f4a63; opacity: .9; }

.dim { fill: none; stroke: var(--blue-500); stroke-width: 3; }
.dim-label {
  font-size: 22px;
  fill: var(--blue-700);
  font-family: inherit;
  font-weight: 600;
}
.arrow-head { fill: var(--blue-500); }

.dim-focal, .dim-focal-label { stroke: #c76f36; fill: #c76f36; }
.dim-wd, .dim-wd-label { stroke: #5b7fa3; fill: #5b7fa3; }
.dim-aovh { stroke: #4f9d69; }
.dim-aovh-label { fill: #4f9d69; }
.dim-ow, .dim-ow-label { stroke: #a15bb0; fill: #a15bb0; }
.dim-oh, .dim-oh-label { stroke: #c2534f; fill: #c2534f; }

/* ===== Fields ===== */
.fields-grid {
  display: grid;
  grid-template-columns: 1fr;
  gap: 14px;
}
@media (min-width: 520px) {
  .fields-grid { grid-template-columns: 1fr 1fr; }
}

.field label {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 13.5px;
  font-weight: 600;
  color: var(--ink-soft);
  margin-bottom: 6px;
}
.dot {
  width: 9px;
  height: 9px;
  border-radius: 50%;
  flex: none;
}
.dot-focal { background: #c76f36; }
.dot-wd { background: #5b7fa3; }
.dot-ow { background: #a15bb0; }
.dot-oh { background: #c2534f; }
.dot-aovh { background: #4f9d69; }
.dot-aovv { background: #d3a52a; }

.input-unit {
  position: relative;
  display: flex;
  align-items: center;
}
.input-unit input {
  width: 100%;
  height: 44px;
  border-radius: 10px;
  border: 1px solid var(--line);
  padding: 0 44px 0 14px;
  font-size: 16px;
  color: var(--ink);
  background: #fff;
  -moz-appearance: textfield;
}
.input-unit input::-webkit-outer-spin-button,
.input-unit input::-webkit-inner-spin-button {
  -webkit-appearance: none;
  margin: 0;
}
.input-unit input:focus {
  outline: none;
  border-color: var(--blue-500);
  box-shadow: 0 0 0 3px var(--blue-100);
}
.input-unit .unit {
  position: absolute;
  right: 14px;
  font-size: 13px;
  color: #9aa7b0;
  pointer-events: none;
}
.field.is-calculated .input-unit input {
  background: var(--blue-050);
  color: var(--blue-700);
  font-weight: 600;
  border-color: var(--blue-100);
}
.field.is-calculated label::after {
  content: 'calculated';
  margin-left: auto;
  font-size: 10.5px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: .04em;
  color: var(--blue-500);
  background: var(--blue-100);
  padding: 2px 7px;
  border-radius: 999px;
}

.result-note {
  margin-top: 16px;
  font-size: 13.5px;
  color: var(--ink-soft);
  min-height: 1.2em;
}
.result-note.warn { color: var(--orange-dark); }

/* ===== Lens picker ===== */
.lens-picker {
  margin-top: 10px;
}
.lens-picker-label {
  display: block;
  font-size: 11.5px;
  color: var(--ink-soft);
  margin-bottom: 5px;
}
.lens-picker-buttons {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 6px;
}
.lens-pick {
  min-width: 34px;
  height: 28px;
  padding: 0 8px;
  border-radius: 999px;
  border: 1px solid var(--line);
  background: #fff;
  color: var(--ink-soft);
  font-size: 12.5px;
  font-weight: 600;
  cursor: pointer;
}
.lens-pick:hover { border-color: var(--blue-300); color: var(--blue-700); }
.lens-pick.active {
  background: var(--blue-500);
  border-color: var(--blue-500);
  color: #fff;
}
.lens-picker-unit {
  font-size: 11.5px;
  color: var(--ink-soft);
  margin-left: 2px;
}

/* ===== Vision precision ===== */
.vision-precision {
  margin-top: 16px;
  padding: 14px 16px;
  background: var(--blue-050);
  border: 1px solid var(--blue-100);
  border-radius: 10px;
}
.vision-precision h3 {
  margin: 0 0 10px;
  font-size: 12.5px;
  text-transform: uppercase;
  letter-spacing: .04em;
  color: var(--blue-700);
}
.vp-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
}
.vp-item {
  display: flex;
  flex-direction: column;
  gap: 3px;
}
.vp-label {
  font-size: 12px;
  color: var(--ink-soft);
}
.vp-value {
  font-size: 18px;
  font-weight: 700;
  color: var(--blue-900);
}
.vp-note {
  margin: 10px 0 0;
  font-size: 11.5px;
  color: var(--ink-soft);
  line-height: 1.5;
}

/* ===== Sensor card ===== */
.sensor-empty {
  color: var(--ink-soft);
  font-size: 14px;
  padding: 10px 0 4px;
}
.sensor-table {
  width: 100%;
  border-collapse: collapse;
  margin-top: 8px;
}
.sensor-table td {
  padding: 10px 4px;
  border-bottom: 1px solid var(--line);
  font-size: 14px;
  vertical-align: top;
}
.sensor-table td:first-child {
  color: var(--ink-soft);
  width: 45%;
}
.sensor-table td:last-child {
  color: var(--ink);
  font-weight: 600;
  text-align: right;
}
.sensor-table tr:last-child td { border-bottom: none; }

/* ===== Modal ===== */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(20, 30, 40, .45);
  display: flex;
  align-items: flex-end;
  justify-content: center;
  z-index: 50;
  padding: 0;
}
.modal-overlay[hidden] {
  display: none;
}
@media (min-width: 720px) {
  .modal-overlay { align-items: center; padding: 24px; }
}

.modal {
  background: #fff;
  width: 100%;
  max-width: 720px;
  max-height: 92vh;
  overflow-y: auto;
  border-radius: 18px 18px 0 0;
  padding: 20px 20px calc(20px + env(safe-area-inset-bottom));
  box-shadow: 0 -8px 30px rgba(0,0,0,.2);
}
@media (min-width: 720px) {
  .modal { border-radius: 16px; }
}

.modal-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 8px;
}
.modal-head h2 { margin: 0; font-size: 18px; color: var(--blue-900); }
.btn-icon {
  border: none;
  background: transparent;
  width: 32px;
  height: 32px;
  border-radius: 8px;
  color: var(--ink-soft);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
}
.btn-icon svg { width: 20px; height: 20px; }
.btn-icon:hover { background: var(--blue-050); }

.form-section { margin-top: 18px; }
.form-section h3 {
  font-size: 12.5px;
  text-transform: uppercase;
  letter-spacing: .04em;
  color: var(--blue-700);
  margin: 0 0 10px;
}
.form-grid {
  display: grid;
  grid-template-columns: 1fr;
  gap: 12px;
}
@media (min-width: 560px) {
  .form-grid { grid-template-columns: 1fr 1fr; }
  .form-field-wide { grid-column: 1 / -1; }
}

.form-field {
  display: flex;
  flex-direction: column;
  gap: 6px;
  font-size: 13px;
  font-weight: 600;
  color: var(--ink-soft);
}
.form-field input,
.form-field select {
  height: 42px;
  border-radius: 10px;
  border: 1px solid var(--line);
  padding: 0 12px;
  font-size: 15px;
  font-weight: 400;
  color: var(--ink);
  background: #fff;
  width: 100%;
}
.form-field .input-unit input { padding-right: 42px; }
.form-field input:focus,
.form-field select:focus {
  outline: none;
  border-color: var(--blue-500);
  box-shadow: 0 0 0 3px var(--blue-100);
}
.form-field input[readonly] { background: var(--blue-050); color: var(--ink-soft); }

.modal-actions {
  display: flex;
  justify-content: flex-end;
  gap: 10px;
  margin-top: 22px;
  padding-top: 16px;
  border-top: 1px solid var(--line);
}
.btn-primary, .btn-secondary {
  height: 42px;
  padding: 0 20px;
  border-radius: 10px;
  font-size: 14.5px;
  font-weight: 700;
  cursor: pointer;
  border: 1px solid transparent;
}
.btn-primary {
  background: var(--blue-500);
  color: #fff;
}
.btn-primary:hover { background: var(--blue-700); }
.btn-secondary {
  background: #fff;
  border-color: var(--line);
  color: var(--ink-soft);
}
.btn-secondary:hover { background: var(--blue-050); }

.saved-cameras {
  margin-top: 22px;
  padding-top: 16px;
  border-top: 1px solid var(--line);
}
.saved-cameras h3 {
  font-size: 12.5px;
  text-transform: uppercase;
  letter-spacing: .04em;
  color: var(--blue-700);
  margin: 0 0 10px;
}
.saved-cameras-list {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 8px;
  max-height: 220px;
  overflow-y: auto;
}
.saved-cameras-list li {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 10px;
  border: 1px solid var(--line);
  border-radius: 10px;
  font-size: 13.5px;
}
.saved-cameras-list .cam-label { flex: 1; }
.saved-cameras-list .cam-label b { color: var(--blue-900); }
.saved-cameras-list .cam-label span { color: var(--ink-soft); display: block; font-size: 12px; }
.saved-cameras-list .del-btn {
  border: none;
  background: transparent;
  color: #c2534f;
  cursor: pointer;
  width: 28px;
  height: 28px;
  border-radius: 6px;
  display: flex;
  align-items: center;
  justify-content: center;
}
.saved-cameras-list .del-btn:hover { background: #fbeceb; }
.saved-cameras-list .del-btn svg { width: 16px; height: 16px; }

/* ===== Print / PDF export ===== */
@media print {
  .no-print { display: none !important; }
  body { background: #fff; }
  .app { max-width: none; padding: 0; }
  .content { display: block; }
  .card {
    box-shadow: none;
    border: none;
    padding: 0;
    margin-bottom: 18px;
    break-inside: avoid;
  }
  .card-actions { display: none !important; }
  .input-unit input {
    border-color: #ccc;
    color: #000;
  }
  .field.is-calculated .input-unit input {
    background: #f2f2f2;
    color: #000;
  }
  .vision-precision { background: #f7f7f7; border-color: #ccc; }
}
.saved-cameras-empty { color: var(--ink-soft); font-size: 13px; }
