:root {
  --bg: #f5f7fb;
  --panel: #fff;
  --text: #1d2433;
  --muted: #667085;
  --primary: #1f6feb;
  --ok: #12a150;
  --warn: #f59e0b;
  --stop: #dc2626;
  --line: #d9e1ef;
}

* { box-sizing: border-box; }

[hidden] { display: none !important; }

html { min-width: 320px; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: Inter, Segoe UI, Arial, sans-serif;
}

.app-header {
  display: flex;
  justify-content: space-between;
  gap: 24px;
  align-items: center;
  padding: 18px 24px;
  background: linear-gradient(135deg, #12213f, #2554a6);
  color: white;
  position: relative;
}

.eyebrow {
  text-transform: uppercase;
  letter-spacing: .12em;
  font-size: .75rem;
  opacity: .8;
}

.app-header h1 {
  margin: .2rem 0;
  font-size: clamp(1.45rem, 3vw, 2.35rem);
}

.app-header p { max-width: 820px; }
.header-copy > p:last-child { margin-bottom: 0; }

.header-actions,
.inline-fields,
.actions {
  display: flex;
  gap: 12px;
  flex-wrap: wrap;
}

.header-actions {
  justify-content: flex-end;
  flex-direction: column;
}

.menu-toggle {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  background: rgba(255, 255, 255, .14);
  border: 1px solid rgba(255, 255, 255, .28);
  color: #fff;
  white-space: nowrap;
}

.app-menu {
  position: absolute;
  right: 24px;
  top: calc(100% - 8px);
  z-index: 5;
  width: min(340px, calc(100vw - 32px));
  padding: 14px;
  border: 1px solid var(--line);
  border-radius: 18px;
  background: var(--panel);
  color: var(--text);
  box-shadow: 0 18px 44px rgba(15, 23, 42, .22);
}

.menu-section + .menu-section {
  margin-top: 14px;
  padding-top: 14px;
  border-top: 1px solid var(--line);
}

.menu-section strong {
  display: block;
  margin-bottom: 8px;
  color: var(--muted);
  font-size: .82rem;
  text-transform: uppercase;
  letter-spacing: .08em;
}

.app-menu button { width: 100%; }

.mobile-menu-nav {
  display: none;
  gap: 8px;
}

.mobile-menu-nav button {
  background: #eef4ff;
  color: #164a9a;
  text-align: left;
}

.tabs {
  display: flex;
  gap: 8px;
  padding: 12px 24px;
  background: #fff;
  border-bottom: 1px solid var(--line);
  position: sticky;
  top: 0;
  z-index: 2;
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
}

.tab,
button {
  border: 0;
  border-radius: 10px;
  background: var(--primary);
  color: #fff;
  padding: 10px 14px;
  font-weight: 700;
  cursor: pointer;
  min-height: 42px;
  touch-action: manipulation;
}

button i {
  margin-right: 6px;
}

.tab {
  background: #eef4ff;
  color: #164a9a;
  white-space: nowrap;
}

.tab.active { background: var(--primary); color: white; }
.secondary { background: #eef4ff; color: #164a9a; }
.danger { background: #fee2e2; color: #991b1b; }

main { padding: 24px; }
.view { display: none; }
.view.active { display: block; }

.section-title {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 20px;
  margin-bottom: 18px;
}

.section-title h2 { margin: 0; }
.section-title p,
.muted { color: var(--muted); }

.form-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(240px, 1fr));
  gap: 16px;
}

.panel,
.card {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 18px;
  padding: 18px;
  box-shadow: 0 10px 30px rgba(31, 48, 80, .06);
}

#referentials .form-grid,
#routingForm,
#orderForm {
  display: none;
}

.form-modal {
  position: fixed;
  inset: 0;
  z-index: 20;
  display: grid;
  place-items: start center;
  padding: 24px;
  background: rgba(15, 23, 42, .55);
  overflow: auto;
}

.form-modal-dialog {
  width: min(1120px, 100%);
  background: var(--panel);
  border-radius: 22px;
  padding: 20px;
  box-shadow: 0 24px 80px rgba(15, 23, 42, .35);
}

.form-modal .form-grid {
  display: grid !important;
}

.form-modal form,
.form-modal #routingForm,
.form-modal #orderForm {
  display: block !important;
}

.add-card {
  display: grid;
  place-items: center;
  min-height: 220px;
  border: 2px dashed var(--line);
  background: #f8fafc;
}

.add-card > button {
  display: grid;
  place-items: center;
  gap: 8px;
  width: 100%;
  min-height: 130px;
  background: transparent;
  color: var(--primary);
  border: 0;
}

.add-card > button span {
  display: grid;
  place-items: center;
  width: 72px;
  height: 72px;
  border-radius: 50%;
  background: #dbeafe;
  font-size: 3rem;
  line-height: 1;
}

.wide { margin-bottom: 18px; }
.panel h3 { margin-top: 0; }

input,
select,
textarea {
  width: 100%;
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 10px;
  margin: 6px 0 10px;
  background: #fff;
  color: var(--text);
  font: inherit;
  min-height: 42px;
}

textarea {
  min-height: 76px;
  resize: vertical;
}

.field {
  display: block;
  font-weight: 700;
}

.field small {
  display: block;
  color: var(--muted);
  font-weight: 400;
  line-height: 1.35;
  margin: -4px 0 10px;
}

.camera-field {
  display: block;
  border: 1px dashed #b7c7df;
  border-radius: 14px;
  padding: 10px;
  margin: 6px 0 12px;
  background: #f8fbff;
}

.camera-field small {
  display: block;
  color: var(--muted);
  line-height: 1.35;
}

.photo-preview {
  display: none;
  width: 100%;
  max-height: 180px;
  object-fit: contain;
  border-radius: 12px;
  margin-top: 8px;
  background: #edf2f7;
}

.photo-preview.visible { display: block; }

.inline-fields > * { flex: 1 1 180px; }

.cards-grid,
.lists-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(280px, 1fr));
  gap: 16px;
}

.lists-grid { margin-top: 18px; }
.card h3 { margin: .2rem 0; }

.photo {
  width: 100%;
  height: 140px;
  object-fit: contain;
  border-radius: 12px;
  background: #edf2f7;
  margin-bottom: 10px;
}

.badge {
  display: inline-flex;
  align-items: center;
  border-radius: 999px;
  padding: 4px 9px;
  font-size: .8rem;
  font-weight: 800;
  margin: 3px;
  background: #eef2ff;
  color: #3730a3;
}

.badge.ok { background: #dcfce7; color: #166534; }
.badge.warn { background: #fef3c7; color: #92400e; }
.badge.stop { background: #fee2e2; color: #991b1b; }

.metrics {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
  margin: 12px 0;
}

.metric {
  background: #f8fafc;
  border-radius: 12px;
  padding: 10px;
}

.metric strong {
  display: block;
  font-size: 1.3rem;
}

.progress {
  height: 12px;
  background: #e5e7eb;
  border-radius: 999px;
  overflow: hidden;
}

.progress span {
  display: block;
  height: 100%;
  background: linear-gradient(90deg, #12a150, #1f6feb);
}

.actions { margin-top: 12px; }
.actions button { padding: 8px 10px; }

.event-log {
  background: #0f172a;
  color: #dbeafe;
  border-radius: 16px;
  padding: 16px;
  max-height: 70vh;
  overflow: auto;
}

.event {
  border-bottom: 1px solid rgba(255, 255, 255, .12);
  padding: 10px 0;
}

.event time { color: #93c5fd; }
.empty { color: var(--muted); font-style: italic; }
.small { font-size: .86rem; }

.list-item {
  border-top: 1px solid var(--line);
  padding: 10px 0;
}

.list-item:first-child { border-top: 0; }

@media (max-width: 760px) {
  .app-header {
    align-items: flex-start;
    gap: 12px;
    padding: 14px 16px;
  }

  .section-title,
  .inline-fields,
  .actions {
    flex-direction: column;
  }

  .header-copy > p:last-child { display: none; }

  .menu-toggle {
    position: absolute;
    top: 14px;
    right: 16px;
    min-height: 40px;
    padding: 9px 11px;
  }

  .menu-toggle span { display: none; }

  .app-menu {
    left: 12px;
    right: 12px;
    top: calc(100% - 2px);
    width: auto;
  }

  .mobile-menu-nav { display: grid; }

  .tabs { display: none; }

  .section-title button,
  .actions button,
  .inline-fields > * {
    width: 100%;
  }

  main { padding: 14px; }
  .metrics { grid-template-columns: 1fr; }
  .operation-material-row { grid-template-columns: 1fr; }
}

@media (max-width: 480px) {
  .form-grid,
  .cards-grid,
  .lists-grid {
    grid-template-columns: 1fr;
  }

  .panel,
  .card {
    border-radius: 14px;
    padding: 14px;
  }

  .app-header h1 { font-size: 1.7rem; }
}

.spc-builder {
  border: 1px solid var(--line);
  border-radius: 16px;
  padding: 14px;
  margin: 14px 0;
  background: #fbfdff;
}

.spc-builder h3 { margin: 0 0 4px; }

.spc-controls-editor {
  display: grid;
  gap: 12px;
  margin: 12px 0;
}

.spc-control-row {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
  gap: 10px;
  align-items: end;
  border: 1px dashed #b7c7df;
  border-radius: 14px;
  padding: 12px;
  background: #fff;
}

.spc-control-row button { align-self: center; }

.machine-actions {
  border-top: 1px solid var(--line);
  margin-top: 14px;
  padding-top: 12px;
}

.machine-actions strong { width: 100%; }

.machine-order-panel {
  border: 1px solid var(--line);
  border-radius: 14px;
  padding: 12px;
  margin: 12px 0;
  background: #fbfdff;
}

.machine-order-title {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  flex-wrap: wrap;
}

.reason-overlay {
  position: fixed;
  inset: 0;
  z-index: 20;
  display: grid;
  place-items: center;
  padding: 18px;
  background: rgba(15, 23, 42, .55);
}

.reason-dialog {
  width: min(560px, 100%);
  max-height: 90vh;
  overflow: auto;
  background: #fff;
  border-radius: 18px;
  padding: 18px;
  box-shadow: 0 20px 70px rgba(0, 0, 0, .28);
}

.reason-dialog h3 { margin-top: 0; }

.help-button {
  white-space: nowrap;
}

.help-dialog {
  width: min(760px, 100%);
}

.help-content {
  display: grid;
  gap: 12px;
  line-height: 1.55;
}

.help-content p,
.help-content ul {
  margin: 0;
}

.help-content h4 {
  margin: 8px 0 0;
}

.help-content a {
  color: var(--primary);
  font-weight: 800;
}

.reason-options {
  display: grid;
  gap: 10px;
  margin: 14px 0;
}

.reason-options label {
  display: flex;
  gap: 10px;
  align-items: center;
  padding: 10px;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: #f8fafc;
}

.reason-options input { width: auto; margin: 0; min-height: auto; }

.routing-header {
  border: 1px solid var(--line);
  border-radius: 16px;
  padding: 14px;
  margin-bottom: 16px;
  background: #f8fbff;
}

.routing-header h3 { margin-top: 0; }

.operation-box {
  border: 1px solid #b7c7df;
  border-radius: 18px;
  padding: 16px;
  margin: 0;
  background: #fff;
}

.operation-box legend {
  padding: 0 8px;
  font-weight: 800;
}

.operation-list {
  display: grid;
  gap: 8px;
  margin-top: 10px;
}

.operation-materials {
  margin: 12px 0;
  padding: 14px;
  border: 1px solid var(--line);
  border-radius: 16px;
  background: #f8fafc;
}

.section-title.compact {
  margin: 0 0 10px;
  padding: 0;
  align-items: flex-start;
}

.operation-materials-editor {
  display: grid;
  gap: 10px;
}

.operation-material-row {
  display: grid;
  grid-template-columns: minmax(180px, 1fr) minmax(180px, 1fr) auto;
  gap: 10px;
  align-items: end;
}

.spc-chart {
  width: 100%;
  height: 180px;
  margin: 12px 0;
}

.spc-chart rect {
  fill: #f8fafc;
  stroke: var(--line);
}

.spc-chart polyline {
  fill: none;
  stroke: var(--primary);
  stroke-width: 3;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.spc-chart circle { fill: var(--primary); }

.spc-chart .it {
  stroke: var(--stop);
  stroke-width: 1.5;
  stroke-dasharray: 6 5;
}

.spc-chart .forecast {
  stroke: var(--warn);
  stroke-width: 2;
  stroke-dasharray: 4 4;
}

.spc-summary-card {
  grid-column: 1 / -1;
  border-color: rgba(37, 99, 235, .25);
  background: linear-gradient(135deg, #ffffff 0%, #eff6ff 100%);
}

.spc-summary-card h3 {
  display: flex;
  gap: 8px;
  align-items: center;
}

.spc-hue-preview {
  display: flex;
  gap: 14px;
  align-items: center;
  margin: 6px 0 10px;
}

.spc-hue-wheel {
  position: relative;
  display: grid;
  place-items: center;
  width: 96px;
  height: 96px;
  border-radius: 50%;
  background: conic-gradient(red, yellow, lime, cyan, blue, magenta, red);
  border: 2px solid var(--line);
}

.spc-hue-wheel::after {
  content: "";
  width: 44px;
  height: 44px;
  border-radius: 50%;
  background: var(--panel);
  border: 1px solid var(--line);
}

.spc-hue-marker {
  position: absolute;
  top: 50%;
  left: 50%;
  width: 14px;
  height: 14px;
  border-radius: 50%;
  border: 3px solid #111827;
  background: #fff;
  box-shadow: 0 1px 6px rgba(0, 0, 0, .25);
}

.spc-hue-swatch {
  width: 64px;
  height: 64px;
  border: 2px solid var(--line);
  border-radius: 14px;
  box-shadow: inset 0 0 0 1px rgba(255,255,255,.35);
}

.spc-color-field input[type="range"] {
  accent-color: var(--primary);
}

.spc-entry-dialog {
  max-width: 760px;
}

.spc-entry-list {
  display: grid;
  gap: 12px;
  max-height: min(70vh, 560px);
  overflow: auto;
  padding-right: 4px;
}

.spc-entry-option {
  border: 1px solid var(--line);
  border-radius: 14px;
  padding: 12px;
  background: #f8fafc;
}

.spc-entry-option input[type="number"] {
  width: 100%;
  margin-top: 8px;
}

.spc-entry-color-preview {
  display: flex;
  gap: 12px;
  margin: 10px 0;
}

.spc-entry-color-preview span {
  display: grid;
  gap: 4px;
  min-width: 84px;
}

.spc-entry-color-preview i {
  display: block;
  width: 72px;
  height: 44px;
  border: 2px solid var(--line);
  border-radius: 12px;
}

.spc-periodicity-field {
  display: grid;
  gap: 6px;
}

.spc-periodicity-field button {
  justify-self: stretch;
  width: 100%;
  margin-top: 2px;
}

.spc-sampling-dialog {
  max-width: 820px;
}

.sampling-result {
  padding: 12px;
  border-radius: 12px;
  background: #eef4ff;
  font-weight: 700;
}
