/* App layout + component styles. Phase 6 — full UI shell. */

body {
  display: grid;
  grid-template-rows: var(--header-h) 1fr;
  grid-template-columns: minmax(260px, 320px) minmax(0, 1fr) minmax(260px, 320px);
  grid-template-areas:
    "header header header"
    "left   center right";
  min-height: 100vh;
  max-width: 100vw;
  overflow-x: hidden;
}

/* ──── Header ──────────────────────────────────────────────────────────── */

.app-header {
  grid-area: header;
  display: flex;
  align-items: center;
  gap: var(--sp-4);
  row-gap: var(--sp-2);
  padding: var(--sp-2) var(--sp-5);
  border-bottom: 1px solid var(--border-default);
  background: var(--bg-surface);
}

.app-title {
  font-family: var(--font-sans);
  font-size: var(--fs-h2);
  font-weight: 600;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: var(--text-primary);
}

.app-title small {
  font-family: var(--font-mono);
  font-weight: 400;
  font-size: var(--fs-caption);
  color: var(--text-tertiary);
  margin-left: var(--sp-2);
  letter-spacing: 0;
  text-transform: none;
}

.help-btn {
  width: 28px;
  height: 28px;
  min-height: 28px;
  min-width: 28px;
  padding: 0;
  border-radius: 50%;
  border: 1px solid var(--border-default);
  background: var(--bg-surface-2);
  color: var(--text-secondary);
  font-family: var(--font-sans);
  font-size: 14px;
  font-weight: 600;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  flex-shrink: 0;
}

.help-btn:hover, .help-btn:active {
  color: var(--accent-hot);
  border-color: var(--accent-hot-dim);
}

.header-spacer {
  flex: 1;
}

.seg {
  display: flex;
  border: 1px solid var(--border-default);
  border-radius: var(--r-md);
  overflow: hidden;
  background: var(--bg-surface-2);
}

.seg button {
  padding: 10px 18px;
  min-height: 40px;
  min-width: 56px;
  font-family: var(--font-sans);
  font-size: var(--fs-small);
  color: var(--text-secondary);
  border: none;
  background: transparent;
  cursor: pointer;
}

.seg button:hover {
  color: var(--text-primary);
  background: var(--bg-surface-2);
}

.seg button[aria-pressed="true"] {
  background: var(--accent-hot-glow);
  color: var(--accent-hot);
}

.seg button:active {
  background: rgba(255, 122, 26, 0.35);
  color: var(--text-primary);
}

.header-btn:active {
  background: rgba(255, 122, 26, 0.35);
  color: var(--text-primary);
}

.header-btn {
  padding: 10px 14px;
  min-height: 40px;
  min-width: 44px;
  font-family: var(--font-sans);
  font-size: var(--fs-small);
  color: var(--text-secondary);
  border: 1px solid var(--border-default);
  background: var(--bg-surface-2);
  border-radius: var(--r-md);
  cursor: pointer;
}

.header-btn:hover:not(:disabled) {
  color: var(--text-primary);
  border-color: var(--border-strong);
}

.header-btn:disabled {
  opacity: 0.4;
  cursor: not-allowed;
}

.header-btn.flash-ok {
  border-color: var(--ok);
  color: var(--ok);
}

.canvas-badge {
  position: absolute;
  top: 12px;
  right: 12px;
  font-family: var(--font-mono);
  font-size: 10px;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--text-tertiary);
  background: rgba(14, 16, 20, 0.65);
  border: 1px solid var(--border-subtle);
  padding: 2px 8px;
  border-radius: var(--r-sm);
  pointer-events: none;
  transition: opacity 200ms ease-out;
}

.canvas-badge.computing { color: var(--accent-cool); }
.canvas-badge.preview   { color: var(--text-tertiary); }
.canvas-badge.hidden    { opacity: 0; }

/* ──── Side panels ─────────────────────────────────────────────────────── */

.left-panel {
  grid-area: left;
  padding: var(--sp-5);
  border-right: 1px solid var(--border-default);
  background: var(--bg-surface);
  overflow-y: auto;
}

.right-panel {
  grid-area: right;
  padding: var(--sp-5);
  border-left: 1px solid var(--border-default);
  background: var(--bg-surface);
  overflow-y: auto;
}

.center-panel {
  grid-area: center;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: var(--sp-5);
  background: var(--bg-app);
}

.panel-section {
  margin-bottom: var(--sp-5);
}

.panel-section h3 {
  font-family: var(--font-sans);
  font-size: var(--fs-caption);
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--text-tertiary);
  margin: 0 0 var(--sp-3) 0;
  display: flex;
  align-items: baseline;
  gap: var(--sp-2);
}

.panel-section h3 .hint {
  text-transform: none;
  letter-spacing: 0;
  font-weight: 400;
  font-size: var(--fs-caption);
  color: var(--text-tertiary);
  opacity: 0.7;
}

/* ──── Form fields ─────────────────────────────────────────────────────── */

.field {
  display: flex;
  flex-direction: column;
  margin-bottom: var(--sp-3);
}

.field label {
  font-size: var(--fs-caption);
  color: var(--text-secondary);
  margin-bottom: 2px;
}

.field input[type="range"] {
  width: 100%;
  accent-color: var(--accent-hot);
}

.field input[type="number"] {
  background: var(--bg-surface-2);
  border: 1px solid var(--border-default);
  border-radius: var(--r-sm);
  padding: 6px 8px;
  font-family: var(--font-mono);
  color: var(--text-primary);
  width: 100%;
}

.field input[type="number"]:focus {
  outline: none;
  border-color: var(--accent-hot);
}

.field-row {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: var(--sp-2);
  align-items: center;
}

.unit-label {
  font-family: var(--font-mono);
  color: var(--text-tertiary);
  font-size: var(--fs-small);
}

/* Hide irrelevant dimension fields based on geometry */
body[data-geometry="cylindrical"] .field-rect { display: none; }
body[data-geometry="rectangular"] .field-cyl { display: none; }

/* ──── Canvas stack ────────────────────────────────────────────────────── */

.canvas-stack {
  position: relative;
  width: min(720px, 100%, calc(100vh - 120px));
  aspect-ratio: 1;
  border: 1px solid var(--border-subtle);
  border-radius: var(--r-md);
  overflow: hidden;
  background: var(--bg-canvas);
}

.center-panel { min-width: 0; }

.canvas-stack canvas {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  display: block;
}

#flow-canvas {
  pointer-events: none;
  mix-blend-mode: screen;
}

#perf-stat {
  font-family: var(--font-mono);
  font-size: var(--fs-caption);
  color: var(--text-tertiary);
  margin: var(--sp-3) 0 0 0;
  font-variant-numeric: tabular-nums;
  text-transform: lowercase;
  letter-spacing: 0.04em;
}

/* ──── Layer stack ─────────────────────────────────────────────────────── */

#layer-stack { display: flex; flex-direction: column; gap: var(--sp-2); }

ul.layers {
  list-style: none;
  padding: 0;
  margin: 0;
  display: flex;
  flex-direction: column;
  gap: var(--sp-2);
}

.layer-row {
  display: grid;
  grid-template-columns: auto 1fr auto;
  gap: var(--sp-2);
  background: var(--bg-surface-2);
  border: 1px solid var(--border-subtle);
  border-radius: var(--r-sm);
  padding: var(--sp-2);
  align-items: center;
}

.layer-row.warn {
  border-color: var(--warn);
}

.layer-row.dragging {
  opacity: 0.45;
}

.layer-row.drop-target {
  border-color: var(--accent-hot);
  box-shadow: 0 0 0 2px var(--accent-hot-glow);
}

.layer-row { cursor: grab; }
.layer-row:active { cursor: grabbing; }
.layer-row select,
.layer-row input,
.layer-row button { cursor: pointer; }
.layer-row input[type="number"] { cursor: text; }

.layer-reorder {
  display: flex;
  flex-direction: column;
  gap: 1px;
}

.layer-reorder button,
.layer-remove {
  width: 22px;
  height: 18px;
  border: 1px solid var(--border-default);
  background: var(--bg-surface);
  color: var(--text-secondary);
  border-radius: 3px;
  font-family: var(--font-mono);
  cursor: pointer;
  padding: 0;
  font-size: 11px;
  display: flex;
  align-items: center;
  justify-content: center;
}

.layer-reorder button:hover:not(:disabled),
.layer-remove:hover {
  color: var(--accent-hot);
  border-color: var(--accent-hot-dim);
}

.layer-reorder button:disabled {
  opacity: 0.3;
  cursor: not-allowed;
}

.layer-remove {
  height: auto;
  align-self: stretch;
  font-size: 12px;
}

.layer-body {
  display: flex;
  flex-direction: column;
  gap: 4px;
  min-width: 0;
}

.layer-body select {
  width: 100%;
  background: var(--bg-surface);
  border: 1px solid var(--border-default);
  border-radius: var(--r-sm);
  padding: 4px 6px;
  color: var(--text-primary);
  font-family: var(--font-sans);
  font-size: var(--fs-small);
}

.layer-body select:focus {
  outline: none;
  border-color: var(--accent-hot);
}

.layer-meta {
  display: grid;
  grid-template-columns: 60px auto 1fr;
  gap: var(--sp-2);
  align-items: center;
  font-size: var(--fs-caption);
  font-family: var(--font-mono);
  color: var(--text-secondary);
}

.layer-meta input[type="number"] {
  background: var(--bg-surface);
  border: 1px solid var(--border-default);
  border-radius: var(--r-sm);
  padding: 2px 4px;
  font-family: var(--font-mono);
  color: var(--text-primary);
  width: 100%;
  font-size: var(--fs-caption);
}

.layer-meta .layer-units {
  color: var(--text-tertiary);
  margin-left: -4px;
}

.layer-meta .layer-temps {
  color: var(--accent-cool);
  white-space: nowrap;
}

.layer-meta .layer-rating {
  color: var(--text-tertiary);
  text-align: right;
  white-space: nowrap;
}

.layer-row.warn .layer-meta .layer-rating {
  color: var(--warn);
}

.add-layer-btn {
  width: 100%;
  border: 1px dashed var(--border-default);
  background: transparent;
  color: var(--text-secondary);
  padding: var(--sp-2);
  border-radius: var(--r-sm);
  font-family: var(--font-sans);
  font-size: var(--fs-small);
  cursor: pointer;
}

.add-layer-btn:hover {
  border-color: var(--accent-hot);
  color: var(--accent-hot);
}

/* ──── Ratings ─────────────────────────────────────────────────────────── */

.rating { margin-bottom: var(--sp-4); }

.rating-head {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  margin-bottom: var(--sp-2);
}

.rating-name {
  font-size: var(--fs-caption);
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--text-secondary);
}

.rating-grade {
  font-family: var(--font-mono);
  font-size: var(--fs-h2);
  font-weight: 600;
  color: var(--accent-hot);
}

.rating-bar {
  height: 6px;
  background: var(--bg-surface-2);
  border-radius: 3px;
  overflow: hidden;
}

.rating-bar-fill {
  height: 100%;
  background: linear-gradient(90deg, var(--accent-hot-dim), var(--accent-hot));
  transition: width 200ms cubic-bezier(0.2, 0.8, 0.2, 1);
}

.rating-driver {
  font-size: var(--fs-caption);
  color: var(--text-tertiary);
  margin-top: var(--sp-2);
  font-family: var(--font-mono);
}

.rating-warn {
  font-size: var(--fs-caption);
  color: var(--warn);
  margin-top: var(--sp-2);
  font-family: var(--font-mono);
}

.meta {
  margin-top: var(--sp-5);
  padding-top: var(--sp-4);
  border-top: 1px solid var(--border-subtle);
  font-size: var(--fs-caption);
  color: var(--text-secondary);
  font-family: var(--font-mono);
  display: flex;
  flex-direction: column;
  gap: var(--sp-2);
}

.meta strong { color: var(--text-primary); }

/* ──── Help dialog ─────────────────────────────────────────────────────── */

dialog#help-dialog {
  margin: auto;
  background: var(--bg-surface);
  color: var(--text-primary);
  border: 1px solid var(--border-default);
  border-radius: var(--r-lg);
  max-width: 720px;
  width: 92vw;
  max-height: 88vh;
  padding: 0;
  font-family: var(--font-sans);
}

dialog#help-dialog::backdrop {
  background: rgba(0, 0, 0, 0.55);
  backdrop-filter: blur(2px);
}

.help-dialog-header {
  position: sticky;
  top: 0;
  z-index: 1;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: var(--sp-3) var(--sp-5);
  border-bottom: 1px solid var(--border-subtle);
  background: var(--bg-surface);
}

.help-dialog-header h2 {
  margin: 0;
  font-size: var(--fs-h2);
  font-weight: 600;
  color: var(--text-primary);
  letter-spacing: 0.02em;
}

.help-close {
  width: 32px;
  height: 32px;
  border-radius: var(--r-md);
  border: 1px solid var(--border-default);
  background: var(--bg-surface-2);
  color: var(--text-secondary);
  font-size: 20px;
  line-height: 1;
  font-family: var(--font-sans);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0;
}

.help-close:hover {
  color: var(--text-primary);
  border-color: var(--border-strong);
}

.help-dialog-body {
  padding: var(--sp-4) var(--sp-5) var(--sp-5);
  overflow-y: auto;
  max-height: calc(88vh - 60px);
  font-size: var(--fs-body);
  line-height: 1.55;
  color: var(--text-secondary);
}

.help-dialog-body h3 {
  font-size: var(--fs-h3);
  font-weight: 600;
  color: var(--text-primary);
  margin: var(--sp-5) 0 var(--sp-3) 0;
  letter-spacing: 0.02em;
}

.help-dialog-body h3:first-of-type {
  margin-top: var(--sp-3);
}

.help-dialog-body p {
  margin: 0 0 var(--sp-3) 0;
}

.help-dialog-body strong {
  color: var(--text-primary);
  font-weight: 600;
}

.help-dialog-body em {
  color: var(--accent-hot);
  font-style: normal;
  font-weight: 500;
}

.help-list {
  margin: 0 0 var(--sp-4) 0;
}

.help-list dt {
  color: var(--text-primary);
  font-weight: 600;
  margin-top: var(--sp-3);
}

.help-list dt:first-child {
  margin-top: 0;
}

.help-list dd {
  margin: var(--sp-1) 0 0 0;
}

.help-list-bullets {
  margin: 0 0 var(--sp-4) 0;
  padding-left: var(--sp-5);
}

.help-list-bullets li {
  margin-bottom: var(--sp-2);
}

.help-table {
  width: 100%;
  border-collapse: collapse;
  margin: 0 0 var(--sp-4) 0;
  font-size: var(--fs-small);
}

.help-table th,
.help-table td {
  text-align: left;
  padding: var(--sp-2) var(--sp-3);
  border-bottom: 1px solid var(--border-subtle);
}

.help-table th {
  color: var(--text-tertiary);
  text-transform: uppercase;
  font-size: var(--fs-caption);
  letter-spacing: 0.06em;
  font-weight: 600;
}

.help-table td:first-child {
  color: var(--text-primary);
  font-weight: 500;
}

.help-dialog-body kbd {
  display: inline-block;
  padding: 2px 6px;
  background: var(--bg-surface-2);
  border: 1px solid var(--border-default);
  border-radius: var(--r-sm);
  font-family: var(--font-mono);
  font-size: var(--fs-caption);
  color: var(--text-primary);
  margin: 0 1px;
}

/* ──── Light theme overrides ───────────────────────────────────────────── */

body.theme-light {
  --bg-app:           #F4F4F2;
  --bg-surface:       #FFFFFF;
  --bg-surface-2:     #F0EFEB;
  --bg-canvas:        #1A1B1F;          /* canvas stays dark for IR contrast */
  --text-primary:     #181A20;
  --text-secondary:   #5C6370;
  --text-tertiary:    #8A8E97;
  --border-subtle:    #E2DFD8;
  --border-default:   #DCD9D2;
  --border-strong:    #B5B0A6;
  --accent-hot:       #D85A0A;
  --accent-hot-dim:   #A14207;
  --accent-cool:      #2F7A99;
  --isotherm:         rgba(0, 0, 0, 0.10);
  --dim-line:         rgba(60, 65, 75, 0.40);
}

/* ──── Responsive ─────────────────────────────────────────────────────── */

/* iPad Pro 12.9" landscape lands here — tighter panels keep the canvas large */
@media (max-width: 1500px) {
  body {
    grid-template-columns: minmax(240px, 280px) minmax(0, 1fr) minmax(240px, 280px);
  }
  .left-panel, .right-panel { padding: var(--sp-4); }
  .app-header {
    flex-wrap: wrap;
    min-height: var(--header-h);
    height: auto;
    padding: var(--sp-2) var(--sp-3);
  }
  body {
    grid-template-rows: auto 1fr;
  }
}

/* iPad Pro portrait, smaller laptops — collapse right panel under canvas */
@media (max-width: 1100px) {
  body {
    grid-template-columns: minmax(240px, 280px) minmax(0, 1fr);
    grid-template-areas:
      "header header"
      "left   center";
  }
  .right-panel {
    grid-area: center;
    border-left: none;
    border-top: 1px solid var(--border-default);
    max-height: 220px;
    overflow-y: auto;
  }
}

/* Phones / very narrow — single column, auto-sized rows so the canvas can't
   overflow upward into the controls or downward into the ratings. */
@media (max-width: 720px) {
  html, body {
    overflow-x: hidden;
    overflow-y: auto;
  }
  body {
    grid-template-columns: 1fr;
    grid-template-rows: auto auto auto auto;
    grid-template-areas:
      "header"
      "left"
      "center"
      "right";
    max-width: 100vw;
  }
  .app-header {
    padding: var(--sp-2) var(--sp-3);
  }
  .left-panel  {
    border-right: none;
    border-bottom: 1px solid var(--border-default);
    padding: var(--sp-3);
    overflow-y: visible;
    max-height: none;
  }
  .right-panel {
    border-left: none;
    border-top: 1px solid var(--border-default);
    padding: var(--sp-3);
    overflow-y: visible;
    max-height: none;
  }
  .center-panel {
    padding: var(--sp-3);
    justify-content: flex-start;
    align-items: center;
  }
  .canvas-stack {
    width: 100%;
    max-width: min(540px, 90vw);
    aspect-ratio: 1;
    height: auto;
  }
  /* Tighten layer stack rows for narrow screens */
  .layer-meta {
    grid-template-columns: 50px auto 1fr;
    font-size: var(--fs-caption);
  }
}
