@import url('https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700;800&family=JetBrains+Mono:wght@400;600&display=swap');

:root {
  --bg: #0c0f1a;
  --sidebar-bg: #111827;
  --sidebar-border: #1e2a3a;
  --main-bg: #0f1622;
  --panel: #161e2e;
  --panel-hover: #1a2436;
  --surface: #1e2a3a;
  --surface2: #243040;

  --text: #e2e8f4;
  --text-muted: #6b7f9a;
  --text-dim: #3d5070;

  --accent: #3b82f6;
  --accent-dark: #2563eb;
  --accent-soft: rgba(59,130,246,0.12);
  --accent-glow: rgba(59,130,246,0.2);

  --teal: #14b8a6;
  --teal-soft: rgba(20,184,166,0.1);

  --danger: #f87171;
  --danger-bg: rgba(248,113,113,0.08);
  --danger-border: rgba(248,113,113,0.3);

  --success: #34d399;

  --line: #1e2a3a;
  --line2: #243040;

  --radius: 8px;
  --radius-sm: 6px;
  --radius-xs: 4px;

  --shadow: 0 4px 24px rgba(0,0,0,0.4);
}

*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

body {
  background: var(--bg);
  color: var(--text);
  font-family: 'Inter', system-ui, sans-serif;
  font-size: 13px;
  line-height: 1.5;
  min-height: 100vh;
  overflow-x: hidden;
}

button, input, select, textarea { font: inherit; }
button { cursor: pointer; border: none; background: none; }

input.mono, .mono { font-family: 'JetBrains Mono', monospace; font-size: 12px; }

/* ─── Layout ─── */
.app-shell {
  display: grid;
  grid-template-columns: 280px 1fr;
  min-height: 100vh;
}

/* ─── Sidebar ─── */
.sidebar {
  background: var(--sidebar-bg);
  border-right: 1px solid var(--sidebar-border);
  display: flex;
  flex-direction: column;
  min-height: 100vh;
  position: sticky;
  top: 0;
  height: 100vh;
  overflow-y: auto;
  overflow-x: hidden;
}

.sidebar-top {
  padding: 16px;
  border-bottom: 1px solid var(--sidebar-border);
  flex-shrink: 0;
}

.logo-block {
  display: flex;
  align-items: center;
  gap: 10px;
}

.logo-mark {
  width: 36px;
  height: 36px;
  background: var(--accent);
  border-radius: var(--radius-sm);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 10px;
  font-weight: 800;
  color: #fff;
  letter-spacing: 0.05em;
  flex-shrink: 0;
}

.logo-title {
  display: block;
  font-size: 13px;
  font-weight: 700;
  color: var(--text);
  line-height: 1.2;
}

.logo-sub {
  display: block;
  font-size: 11px;
  color: var(--text-muted);
  margin-top: 2px;
}

.sidebar-body {
  flex: 1;
  overflow-y: auto;
  padding: 0 0 80px;
}

/* ─── Form sections ─── */
.form-section {
  padding: 14px 16px;
  border-bottom: 1px solid var(--sidebar-border);
}

.form-section-label {
  font-size: 10px;
  font-weight: 800;
  letter-spacing: 0.1em;
  text-transform: uppercase;
  color: var(--text-dim);
  margin-bottom: 10px;
}

.field-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
}

.field {
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.field.wide { grid-column: 1 / -1; }

.field span {
  font-size: 11px;
  font-weight: 600;
  color: var(--text-muted);
  letter-spacing: 0.01em;
}

input, select {
  width: 100%;
  height: 34px;
  background: var(--surface);
  border: 1px solid var(--line2);
  border-radius: var(--radius-xs);
  color: var(--text);
  padding: 0 9px;
  outline: none;
  transition: border-color 150ms, box-shadow 150ms;
  -webkit-appearance: none;
}

input:focus, select:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-glow);
}

input:hover:not(:focus), select:hover:not(:focus) {
  border-color: var(--surface2);
}

select {
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='10' height='6'%3E%3Cpath d='M0 0l5 6 5-6z' fill='%236b7f9a'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 9px center;
  padding-right: 26px;
  cursor: pointer;
}

/* Presets */
.template-row {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin-bottom: 8px;
  min-height: 8px;
}

.template-button {
  height: 28px;
  padding: 0 10px;
  background: var(--surface);
  border: 1px solid var(--line2);
  border-radius: 14px;
  color: var(--teal);
  font-family: 'JetBrains Mono', monospace;
  font-size: 11px;
  font-weight: 600;
  transition: background 150ms, border-color 150ms;
}

.template-button:hover {
  background: var(--teal-soft);
  border-color: var(--teal);
}

.preset-chip {
  display: inline-flex;
  align-items: stretch;
  border-radius: 14px;
  overflow: hidden;
  border: 1px solid var(--line2);
}

.preset-remove-button {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 22px;
  background: var(--surface);
  color: var(--text-muted);
  font-size: 10px;
  border-left: 1px solid var(--line2);
  transition: background 150ms, color 150ms;
}

.preset-remove-button:hover {
  background: var(--danger-bg);
  color: var(--danger);
}

.preset-add-row {
  display: flex;
  gap: 6px;
  align-items: center;
}

.preset-add-row input {
  flex: 1;
}

.btn-add-preset {
  width: 34px;
  height: 34px;
  background: var(--surface);
  border: 1px solid var(--line2);
  border-radius: var(--radius-xs);
  color: var(--text-muted);
  font-size: 16px;
  font-weight: 400;
  transition: background 150ms, color 150ms, border-color 150ms;
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: center;
}

.btn-add-preset:hover {
  background: var(--accent-soft);
  border-color: var(--accent);
  color: var(--accent);
}

/* Checkbox */
.checkbox-field {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  cursor: pointer;
  margin-top: 10px;
  color: var(--text-muted);
  font-size: 12px;
  font-weight: 500;
}

.checkbox-field input[type="checkbox"] {
  width: 15px;
  height: 15px;
  min-height: 0;
  padding: 0;
  border-radius: 3px;
  appearance: auto;
  -webkit-appearance: checkbox;
  accent-color: var(--accent);
  cursor: pointer;
}

/* Error */
.form-error {
  min-height: 18px;
  font-size: 11px;
  font-weight: 600;
  color: var(--danger);
  padding: 0 16px;
  margin-top: 8px;
}

/* Form footer */
.form-footer {
  display: flex;
  gap: 6px;
  padding: 14px 16px;
  border-top: 1px solid var(--sidebar-border);
  position: sticky;
  bottom: 0;
  background: var(--sidebar-bg);
  flex-wrap: wrap;
}

.btn-primary {
  flex: 1;
  min-height: 36px;
  background: var(--accent);
  color: #fff;
  border-radius: var(--radius-sm);
  font-size: 12px;
  font-weight: 700;
  transition: background 150ms, box-shadow 150ms, transform 80ms;
}

.btn-primary:hover {
  background: var(--accent-dark);
  box-shadow: 0 4px 16px var(--accent-glow);
  transform: translateY(-1px);
}

.btn-primary:active { transform: translateY(0); }

.btn-ghost {
  min-height: 36px;
  padding: 0 12px;
  border: 1px solid var(--line2);
  border-radius: var(--radius-sm);
  color: var(--text-muted);
  font-size: 12px;
  font-weight: 600;
  transition: border-color 150ms, color 150ms;
}

.btn-ghost:hover {
  border-color: var(--surface2);
  color: var(--text);
}

.btn-cancel {
  min-height: 36px;
  padding: 0 12px;
  border: 1px solid var(--danger-border);
  border-radius: var(--radius-sm);
  background: var(--danger-bg);
  color: var(--danger);
  font-size: 12px;
  font-weight: 700;
  transition: border-color 150ms;
}

.btn-cancel:hover { border-color: var(--danger); }

.file-input, .is-hidden { display: none !important; }

/* ─── Main area ─── */
.main-area {
  display: flex;
  flex-direction: column;
  min-height: 100vh;
  background: var(--main-bg);
}

/* ─── Topbar ─── */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 10px 20px;
  background: var(--sidebar-bg);
  border-bottom: 1px solid var(--sidebar-border);
  flex-shrink: 0;
}

.topbar-label {
  font-size: 11px;
  font-weight: 700;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--text-dim);
}

.topbar-actions {
  display: flex;
  align-items: center;
  gap: 4px;
  flex-wrap: wrap;
}

.tbtn {
  height: 30px;
  padding: 0 11px;
  background: var(--surface);
  border: 1px solid var(--line2);
  border-radius: var(--radius-xs);
  color: var(--text-muted);
  font-size: 12px;
  font-weight: 600;
  transition: background 150ms, border-color 150ms, color 150ms;
}

.tbtn:hover {
  background: var(--panel-hover);
  border-color: var(--surface2);
  color: var(--text);
}

.tbtn-sep {
  width: 1px;
  height: 20px;
  background: var(--line2);
  margin: 0 4px;
}

.tbtn-danger {
  border-color: var(--danger-border);
  background: var(--danger-bg);
  color: var(--danger);
}

.tbtn-danger:hover {
  border-color: var(--danger);
  background: rgba(248,113,113,0.14);
  color: var(--danger);
}

/* ─── Preview ─── */
.preview-area {
  flex: 1;
  display: flex;
  flex-direction: column;
  padding: 16px 20px;
  min-height: 0;
}

.preview-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 12px;
  flex-wrap: wrap;
}

.preview-header-left {
  display: flex;
  align-items: center;
  gap: 8px;
}

.preview-label {
  font-size: 11px;
  font-weight: 700;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: var(--text-dim);
}

.preview-scale {
  font-size: 11px;
  color: var(--text-dim);
  font-family: 'JetBrains Mono', monospace;
}

.preview-facts {
  display: flex;
  flex-wrap: wrap;
  gap: 5px;
}

.preview-facts span {
  background: var(--surface);
  border: 1px solid var(--line2);
  border-radius: var(--radius-xs);
  padding: 3px 9px;
  font-size: 11px;
  font-weight: 600;
  font-family: 'JetBrains Mono', monospace;
  color: var(--accent);
}

.preview-canvas {
  flex: 1;
  min-height: 300px;
  display: grid;
  place-items: center;
  background: #b8bcc4;
  border: 1px solid #8f96a1;
  border-radius: var(--radius);
  overflow: hidden;
}

.facade-preview-svg {
  display: block;
  width: 100%;
  height: 100%;
  max-height: 560px;
}

.preview-facade {
  fill: none;
  stroke: #000;
  stroke-width: 1.5;
  vector-effect: non-scaling-stroke;
}

.preview-holes circle {
  fill: none;
  stroke: #000;
  stroke-width: 1.5;
  vector-effect: non-scaling-stroke;
}

.preview-dimensions {
  fill: #000;
  font-size: 13px;
  font-weight: 500;
  font-family: 'JetBrains Mono', monospace;
}

.preview-dimensions line {
  stroke: #000;
  stroke-width: 1;
  vector-effect: non-scaling-stroke;
}

.preview-dimensions text {
  dominant-baseline: middle;
  text-anchor: middle;
  fill: #000;
}

.preview-hole-offsets text {
  dominant-baseline: middle;
  fill: #85888f;
  font-family: 'JetBrains Mono', monospace;
  font-size: 12px;
  font-weight: 500;
}

.preview-dimensions .rotated-label { text-anchor: middle; }

.empty-preview {
  color: var(--text-dim);
  font-size: 13px;
  font-weight: 600;
  text-align: center;
}

/* ─── Status bar ─── */
.status-bar {
  padding: 4px 20px;
  min-height: 26px;
}

.status-msg {
  font-size: 11px;
  font-weight: 600;
  color: var(--teal);
  min-height: 16px;
}

/* ─── Table ─── */
.table-section {
  border-top: 1px solid var(--sidebar-border);
  flex-shrink: 0;
  max-height: 260px;
  display: flex;
  flex-direction: column;
}

.table-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 20px;
  border-bottom: 1px solid var(--line);
  flex-shrink: 0;
  background: var(--sidebar-bg);
}

.table-title {
  font-size: 11px;
  font-weight: 700;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: var(--text-dim);
}

.table-count {
  background: var(--accent-soft);
  color: var(--accent);
  font-size: 11px;
  font-weight: 700;
  padding: 2px 9px;
  border-radius: 10px;
  font-family: 'JetBrains Mono', monospace;
}

.table-wrap {
  overflow: auto;
  flex: 1;
}

table {
  width: 100%;
  min-width: 780px;
  border-collapse: collapse;
}

th {
  position: sticky;
  top: 0;
  background: var(--panel);
  color: var(--text-dim);
  font-size: 10px;
  font-weight: 800;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  padding: 8px 14px;
  border-bottom: 1px solid var(--line);
  text-align: left;
  white-space: nowrap;
  z-index: 1;
}

td {
  padding: 8px 14px;
  border-bottom: 1px solid rgba(30,42,58,0.6);
  color: var(--text-muted);
  font-size: 12px;
  white-space: nowrap;
  vertical-align: middle;
}

td:nth-child(2) {
  color: var(--text);
  font-weight: 700;
}

td:nth-child(3),
td:nth-child(5),
td:nth-child(6),
td:nth-child(7),
td:nth-child(8) {
  font-family: 'JetBrains Mono', monospace;
  font-size: 11px;
  color: #4a7fa5;
}

tbody tr { transition: background 100ms; }
tbody tr:hover td { background: var(--panel-hover); }
tbody tr:last-child td { border-bottom: 0; }

@keyframes rowIn {
  from { opacity: 0; transform: translateY(-4px); }
  to   { opacity: 1; transform: translateY(0); }
}

tbody tr.new-row { animation: rowIn 200ms ease forwards; }

.empty-table {
  height: 64px;
  text-align: center;
  color: var(--text-dim);
  font-size: 12px;
}

.action-cell { white-space: nowrap; }
.action-cell .table-button + .table-button { margin-left: 4px; }

.table-button {
  height: 26px;
  padding: 0 9px;
  border: 1px solid var(--line2);
  border-radius: var(--radius-xs);
  background: var(--surface);
  color: var(--text-muted);
  font-size: 11px;
  font-weight: 600;
  transition: background 150ms, border-color 150ms, color 150ms;
}

.table-button:hover {
  background: var(--panel-hover);
  border-color: var(--surface2);
  color: var(--text);
}

.danger-link {
  border-color: var(--danger-border);
  background: var(--danger-bg);
  color: var(--danger);
}

.danger-link:hover {
  border-color: var(--danger);
  background: rgba(248,113,113,0.14);
}

/* ─── Toast ─── */
.toast {
  position: fixed;
  top: 20px;
  right: 20px;
  z-index: 100;
  background: #065f46;
  border: 1px solid #047857;
  border-radius: var(--radius);
  box-shadow: 0 8px 28px rgba(0,0,0,0.5);
  color: #6ee7b7;
  font-size: 12px;
  font-weight: 700;
  padding: 10px 16px;
  opacity: 0;
  pointer-events: none;
  transform: translateY(-10px) scale(0.97);
  transition: opacity 180ms ease, transform 180ms ease;
}

.toast.is-visible {
  opacity: 1;
  transform: translateY(0) scale(1);
}

/* ─── Responsive ─── */
@media (max-width: 860px) {
  .app-shell {
    grid-template-columns: 1fr;
  }

  .sidebar {
    position: static;
    height: auto;
    min-height: auto;
    border-right: none;
    border-bottom: 1px solid var(--sidebar-border);
  }

  .form-footer {
    position: static;
  }

  .main-area { min-height: auto; }

  .table-section { max-height: none; }

  .preview-area { min-height: 320px; }
}

@media (max-width: 540px) {
  .field-grid { grid-template-columns: 1fr; }
  .field.wide { grid-column: auto; }
  .topbar { flex-direction: column; align-items: flex-start; gap: 8px; }
  .topbar-actions { gap: 4px; }
  .tbtn { font-size: 11px; padding: 0 8px; }
}
