/* ════════════════════════════════════════════════════════════════
   APQR Graph Builder — Main Stylesheet
   ════════════════════════════════════════════════════════════════ */

/* ── Reset & Base ───────────────────────────────────────────────── */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --color-primary:     #1a56db;
  --color-primary-h:   #1648c0;
  --color-primary-l:   #e8effe;
  --color-success:     #057a55;
  --color-success-l:   #def7ec;
  --color-danger:      #c81e1e;
  --color-danger-l:    #fde8e8;
  --color-warning:     #c27803;
  --color-warning-l:   #fdf6b2;
  --color-surface:     #ffffff;
  --color-bg:          #f3f4f8;
  --color-panel:       #ffffff;
  --color-border:      #dde1ec;
  --color-border-m:    #c8cddd;
  --color-text:        #1e2230;
  --color-text-2:      #4b5268;
  --color-text-3:      #8590a8;
  --color-ucl:         #e02424;
  --color-lcl:         #1a56db;
  --color-usl:         #ff6b00;
  --color-lsl:         #8b5cf6;
  --color-target:      #059669;
  --radius-sm:         6px;
  --radius:            10px;
  --radius-lg:         14px;
  --shadow-sm:         0 1px 3px rgba(0,0,0,.08);
  --shadow:            0 4px 16px rgba(0,0,0,.10);
  --shadow-lg:         0 8px 32px rgba(0,0,0,.14);
  --transition:        .18s ease;
  --header-h:          58px;
  --panel-w:           370px;
  --font:              'Segoe UI', system-ui, -apple-system, sans-serif;
}

html { font-size: 14px; scroll-behavior: smooth; }

body {
  font-family: var(--font);
  background: var(--color-bg);
  color: var(--color-text);
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}

/* ── Scrollbar ──────────────────────────────────────────────────── */
::-webkit-scrollbar { width: 6px; height: 6px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--color-border-m); border-radius: 99px; }
::-webkit-scrollbar-thumb:hover { background: var(--color-text-3); }

/* ── Header ─────────────────────────────────────────────────────── */
.app-header {
  position: sticky; top: 0; z-index: 100;
  height: var(--header-h);
  background: var(--color-primary);
  color: #fff;
  display: flex; align-items: center; justify-content: space-between;
  padding: 0 20px;
  box-shadow: 0 2px 12px rgba(26,86,219,.35);
}
.header-brand { display: flex; align-items: center; gap: 12px; }
.brand-icon { width: 28px; height: 28px; stroke: #93c5fd; }
.brand-title { font-size: 1.2rem; font-weight: 700; letter-spacing: -.3px; display: block; }
.brand-sub { font-size: .72rem; opacity: .75; display: block; margin-top: -2px; }
.header-actions { display: flex; gap: 8px; }
.header-actions .btn { color: #fff; border-color: rgba(255,255,255,.35); }
.header-actions .btn:hover { background: rgba(255,255,255,.15); }

/* ── Main Layout ─────────────────────────────────────────────────── */
.app-layout {
  flex: 1;
  display: grid;
  grid-template-columns: var(--panel-w) 1fr;
  min-height: calc(100vh - var(--header-h));
  gap: 0;
}

/* ── Control Panel ───────────────────────────────────────────────── */
.control-panel {
  background: var(--color-panel);
  border-right: 1px solid var(--color-border);
  overflow-y: auto;
  display: flex; flex-direction: column;
  position: sticky; top: var(--header-h);
  height: calc(100vh - var(--header-h));
}

/* ── Panel Sections ──────────────────────────────────────────────── */
.panel-section { border-bottom: 1px solid var(--color-border); }

.section-header {
  display: flex; align-items: center; justify-content: space-between;
  padding: 13px 16px;
  cursor: pointer;
  user-select: none;
  font-weight: 600;
  font-size: .82rem;
  letter-spacing: .4px;
  color: var(--color-text-2);
  text-transform: uppercase;
  background: #fafbfd;
  transition: background var(--transition);
}
.section-header:hover { background: var(--color-primary-l); }
.section-header span { display: flex; align-items: center; gap: 6px; }
.chevron { width: 16px; height: 16px; transition: transform var(--transition); }
.chevron.closed { transform: rotate(-90deg); }

.section-body {
  padding: 14px 16px;
  display: flex; flex-direction: column; gap: 10px;
  overflow: hidden;
  transition: max-height .25s ease, padding .25s ease;
}
.section-body.collapsed {
  max-height: 0 !important;
  padding-top: 0; padding-bottom: 0;
  overflow: hidden;
}

/* ── Fields ──────────────────────────────────────────────────────── */
.field-group { display: flex; flex-direction: column; gap: 4px; flex: 1; }
.field-group label {
  font-size: .76rem; font-weight: 600;
  color: var(--color-text-2); letter-spacing: .2px;
}
.field-group label small { font-weight: 400; opacity: .75; }
.field-row { display: flex; gap: 10px; }

input[type="text"],
input[type="number"],
input[type="email"],
select,
textarea {
  width: 100%;
  padding: 7px 10px;
  border: 1px solid var(--color-border);
  border-radius: var(--radius-sm);
  font-size: .85rem; font-family: var(--font);
  color: var(--color-text);
  background: var(--color-surface);
  transition: border-color var(--transition), box-shadow var(--transition);
  outline: none;
}
input:focus, select:focus, textarea:focus {
  border-color: var(--color-primary);
  box-shadow: 0 0 0 3px rgba(26,86,219,.12);
}
input[type="color"].color-input {
  padding: 2px 4px;
  height: 34px; width: 100%;
  cursor: pointer;
  border-radius: var(--radius-sm);
}
input[type="range"].range-input {
  padding: 0; border: none; background: none;
  accent-color: var(--color-primary);
  width: 100%; cursor: pointer;
  box-shadow: none;
}
input[type="range"].range-input:focus { box-shadow: none; }

/* ── Limits Grid ─────────────────────────────────────────────────── */
.limits-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
}
.limit-card {
  background: #fafbfd;
  border: 1px solid var(--color-border);
  border-radius: var(--radius-sm);
  padding: 8px 10px;
  display: flex; flex-direction: column; gap: 4px;
}
.limit-card label { font-size: .72rem; font-weight: 700; letter-spacing: .3px; }
.limit-ucl label { color: var(--color-ucl); }
.limit-lcl label { color: var(--color-lcl); }
.limit-target label { color: var(--color-target); }
.limit-usl label { color: var(--color-usl); }
.limit-lsl label { color: var(--color-lsl); }
.limit-calc { align-items: center; justify-content: center; }

/* ── Data Table ──────────────────────────────────────────────────── */
.data-toolbar {
  display: flex; flex-wrap: wrap; gap: 6px;
  margin-bottom: 4px;
}
.table-wrapper {
  max-height: 260px;
  overflow-y: auto;
  border: 1px solid var(--color-border);
  border-radius: var(--radius-sm);
}
.data-table {
  width: 100%;
  border-collapse: collapse;
  font-size: .82rem;
}
.data-table thead {
  position: sticky; top: 0;
  background: #f0f2f8;
  z-index: 1;
}
.data-table th {
  padding: 7px 8px;
  text-align: left;
  font-weight: 700;
  font-size: .72rem;
  text-transform: uppercase;
  letter-spacing: .3px;
  color: var(--color-text-2);
  border-bottom: 1px solid var(--color-border);
}
.data-table td {
  padding: 4px 6px;
  border-bottom: 1px solid #f0f2f8;
  vertical-align: middle;
}
.data-table tr:hover td { background: var(--color-primary-l); }
.data-table tr.ooc-row td { background: #fff1f1 !important; }
.data-table input {
  padding: 5px 7px;
  font-size: .82rem;
  border-radius: 4px;
}
.row-num {
  display: flex; align-items: center; justify-content: center;
  width: 22px; height: 22px;
  border-radius: 50%;
  background: var(--color-primary-l);
  color: var(--color-primary);
  font-size: .7rem; font-weight: 700;
}
.row-num.ooc { background: var(--color-danger-l); color: var(--color-danger); }
.btn-del-row {
  width: 24px; height: 24px;
  border: none; background: none;
  cursor: pointer; color: var(--color-text-3);
  border-radius: 4px;
  display: flex; align-items: center; justify-content: center;
  transition: background var(--transition), color var(--transition);
}
.btn-del-row:hover { background: var(--color-danger-l); color: var(--color-danger); }

.paste-area-wrapper {
  background: #fafbfd;
  border: 1px solid var(--color-border);
  border-radius: var(--radius-sm);
  padding: 10px;
  margin-top: 4px;
}
.paste-area-wrapper textarea { resize: vertical; font-family: monospace; font-size: .8rem; }
.row-count { font-size: .72rem; color: var(--color-text-3); margin-top: 4px; }

/* ── Panel Actions ───────────────────────────────────────────────── */
.panel-actions {
  padding: 14px 16px;
  display: flex; flex-direction: column; gap: 8px;
  margin-top: auto;
  background: #fafbfd;
  border-top: 1px solid var(--color-border);
  position: sticky; bottom: 0; z-index: 5;
}
.action-row { display: flex; gap: 6px; }
.action-row .btn { flex: 1; }

/* ── Buttons ─────────────────────────────────────────────────────── */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  padding: 8px 14px;
  border-radius: var(--radius-sm);
  font-size: .82rem; font-weight: 600; font-family: var(--font);
  cursor: pointer; border: 1px solid transparent;
  transition: background var(--transition), border-color var(--transition),
              color var(--transition), transform .1s, box-shadow var(--transition);
  white-space: nowrap;
  text-decoration: none;
}
.btn:active { transform: translateY(1px); }
.btn svg { width: 14px; height: 14px; flex-shrink: 0; }
.btn-sm { padding: 5px 10px; font-size: .78rem; }
.btn-lg { padding: 11px 18px; font-size: .9rem; }
.btn-full { width: 100%; }

.btn-primary { background: var(--color-primary); color: #fff; border-color: var(--color-primary); }
.btn-primary:hover { background: var(--color-primary-h); border-color: var(--color-primary-h); box-shadow: 0 2px 8px rgba(26,86,219,.3); }

.btn-outline { background: transparent; color: var(--color-text-2); border-color: var(--color-border-m); }
.btn-outline:hover { background: var(--color-primary-l); color: var(--color-primary); border-color: var(--color-primary); }

.btn-soft { background: var(--color-primary-l); color: var(--color-primary); border-color: var(--color-primary-l); }
.btn-soft:hover { background: #d1dcfa; }

.btn-danger-soft { background: var(--color-danger-l); color: var(--color-danger); border-color: var(--color-danger-l); }
.btn-danger-soft:hover { background: #fbd5d5; }

/* ── Chart Panel ─────────────────────────────────────────────────── */
.chart-panel {
  padding: 22px 24px;
  display: flex; flex-direction: column; gap: 18px;
  overflow-y: auto;
  min-width: 0;
}

.chart-box {
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow);
  overflow: hidden;
  position: relative;
  min-height: 300px;
}
.chart-placeholder {
  display: flex; flex-direction: column;
  align-items: center; justify-content: center;
  min-height: 400px; gap: 14px;
  color: var(--color-text-3);
  padding: 40px;
}
.chart-placeholder svg { width: 90px; height: 68px; }
.chart-placeholder p { font-size: .9rem; text-align: center; }
.chart-canvas-wrapper {
  padding: 10px;
  display: flex; align-items: center; justify-content: center;
}
.chart-canvas-wrapper canvas { max-width: 100%; height: auto; }

/* ── OOC Alert ───────────────────────────────────────────────────── */
.ooc-alert {
  display: flex; align-items: flex-start; gap: 10px;
  padding: 12px 16px;
  background: var(--color-warning-l);
  border: 1px solid #f6c462;
  border-radius: var(--radius);
  color: var(--color-warning);
  font-size: .84rem; font-weight: 500;
}
.ooc-alert svg { width: 18px; height: 18px; flex-shrink: 0; margin-top: 1px; }

/* ── Stats Panel ─────────────────────────────────────────────────── */
.stats-panel {
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow);
  padding: 18px 20px;
}
.stats-title {
  font-size: .88rem; font-weight: 700;
  color: var(--color-text-2);
  text-transform: uppercase; letter-spacing: .4px;
  margin-bottom: 14px; padding-bottom: 10px;
  border-bottom: 1px solid var(--color-border);
}
.stats-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(140px, 1fr));
  gap: 10px;
}
.stat-card {
  background: #fafbfd;
  border: 1px solid var(--color-border);
  border-radius: var(--radius-sm);
  padding: 10px 12px;
  display: flex; flex-direction: column; gap: 2px;
}
.stat-card .stat-label {
  font-size: .7rem; font-weight: 700; letter-spacing: .3px;
  text-transform: uppercase; color: var(--color-text-3);
}
.stat-card .stat-value {
  font-size: 1.18rem; font-weight: 700; color: var(--color-text);
}
.stat-card .stat-sub { font-size: .72rem; color: var(--color-text-3); }
.stat-card.positive .stat-value { color: var(--color-success); }
.stat-card.negative .stat-value { color: var(--color-danger); }
.stat-card.warning  .stat-value { color: var(--color-warning); }

/* ── Export Panel ────────────────────────────────────────────────── */
.export-panel {
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow);
  padding: 18px 20px;
}
.export-header {
  display: flex; align-items: center; justify-content: space-between;
  flex-wrap: wrap; gap: 10px;
  margin-bottom: 10px;
}
.export-header h4 {
  font-size: .95rem; font-weight: 700; color: var(--color-text);
}
.export-actions { display: flex; gap: 8px; flex-wrap: wrap; }
.export-hint {
  font-size: .78rem; color: var(--color-text-2);
  background: var(--color-primary-l);
  border-radius: var(--radius-sm);
  padding: 8px 12px;
  margin-bottom: 12px;
  line-height: 1.5;
}
.export-image-container {
  border: 2px dashed var(--color-border-m);
  border-radius: var(--radius);
  padding: 8px;
  text-align: center;
  background: #fafbfd;
}
.export-image-container img {
  max-width: 100%; border-radius: var(--radius-sm);
  box-shadow: var(--shadow);
  cursor: context-menu;
}

/* ── Modal ───────────────────────────────────────────────────────── */
.modal-overlay {
  position: fixed; inset: 0;
  background: rgba(0,0,0,.45);
  z-index: 200;
  display: flex; align-items: center; justify-content: center;
  padding: 20px;
  backdrop-filter: blur(2px);
  animation: fadeIn .18s ease;
}
@keyframes fadeIn { from { opacity: 0; } to { opacity: 1; } }

.modal {
  background: var(--color-surface);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-lg);
  width: 100%; max-width: 440px;
  display: flex; flex-direction: column;
  animation: slideUp .2s ease;
  max-height: 90vh;
  overflow: hidden;
}
.modal-wide { max-width: 640px; }
@keyframes slideUp { from { transform: translateY(20px); opacity: 0; } to { transform: none; opacity: 1; } }

.modal-header {
  display: flex; align-items: center; justify-content: space-between;
  padding: 16px 20px;
  border-bottom: 1px solid var(--color-border);
}
.modal-header h3 { font-size: 1rem; font-weight: 700; }
.modal-close {
  width: 28px; height: 28px;
  border: none; background: #f0f2f8;
  border-radius: 50%; cursor: pointer;
  font-size: 1.1rem; line-height: 1;
  display: flex; align-items: center; justify-content: center;
  color: var(--color-text-2);
  transition: background var(--transition);
}
.modal-close:hover { background: var(--color-danger-l); color: var(--color-danger); }

.modal-body {
  padding: 18px 20px;
  display: flex; flex-direction: column; gap: 12px;
  overflow-y: auto; flex: 1;
}
.modal-footer {
  padding: 14px 20px;
  border-top: 1px solid var(--color-border);
  display: flex; justify-content: flex-end; gap: 8px;
}

/* ── Saved Configs List ──────────────────────────────────────────── */
.config-item {
  display: flex; align-items: center; gap: 10px;
  padding: 12px 14px;
  border: 1px solid var(--color-border);
  border-radius: var(--radius);
  margin-bottom: 8px;
  transition: border-color var(--transition), background var(--transition);
  cursor: default;
}
.config-item:hover { border-color: var(--color-primary); background: var(--color-primary-l); }
.config-item-info { flex: 1; min-width: 0; }
.config-item-name { font-weight: 700; font-size: .88rem; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.config-item-meta { font-size: .72rem; color: var(--color-text-3); margin-top: 2px; }
.config-item-actions { display: flex; gap: 6px; }
.loading-text { color: var(--color-text-3); font-size: .85rem; padding: 10px; }
.empty-text { color: var(--color-text-3); font-size: .85rem; text-align: center; padding: 24px; }

/* ── Toast Notifications ─────────────────────────────────────────── */
.toast-container {
  position: fixed; bottom: 20px; right: 20px;
  z-index: 300;
  display: flex; flex-direction: column; gap: 8px;
  pointer-events: none;
}
.toast {
  padding: 11px 16px;
  border-radius: var(--radius);
  font-size: .83rem; font-weight: 500;
  box-shadow: var(--shadow-lg);
  animation: slideInRight .25s ease;
  pointer-events: all;
  display: flex; align-items: center; gap: 8px;
  min-width: 220px; max-width: 340px;
}
@keyframes slideInRight {
  from { transform: translateX(60px); opacity: 0; }
  to   { transform: none; opacity: 1; }
}
.toast-success { background: var(--color-success); color: #fff; }
.toast-error   { background: var(--color-danger);  color: #fff; }
.toast-info    { background: var(--color-primary);  color: #fff; }
.toast-warning { background: var(--color-warning-l); color: var(--color-warning); border: 1px solid #f6c462; }

/* ── Utility ─────────────────────────────────────────────────────── */
.hint { font-size: .74rem; color: var(--color-text-3); line-height: 1.5; }
.required { color: var(--color-danger); }
.visually-hidden { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0,0,0,0); }

/* ── Responsive ──────────────────────────────────────────────────── */
@media (max-width: 900px) {
  :root { --panel-w: 320px; }
}
@media (max-width: 700px) {
  .app-layout {
    grid-template-columns: 1fr;
    grid-template-rows: auto 1fr;
  }
  .control-panel {
    position: static;
    height: auto;
    max-height: none;
    overflow-y: visible;
  }
  .chart-panel { padding: 14px; }
  .header-actions .btn span { display: none; }
  .limits-grid { grid-template-columns: 1fr; }
  .stats-grid { grid-template-columns: 1fr 1fr; }
  .export-header { flex-direction: column; align-items: flex-start; }
}
@media (max-width: 480px) {
  .brand-sub { display: none; }
  .field-row { flex-direction: column; }
  .stats-grid { grid-template-columns: 1fr 1fr; }
}

/* ── Print ───────────────────────────────────────────────────────── */
@media print {
  .app-header, .control-panel, .export-panel, .panel-actions { display: none !important; }
  .app-layout { grid-template-columns: 1fr; }
  .chart-panel { padding: 0; }
  .chart-box { box-shadow: none; border: none; }
}
