:root {
  --cv-red: #d62328;
  --ink: #233047;
  --ink-soft: #6d7991;
  --paper: #f6f3ea;
  --panel: #ffffff;
  --line: #d9dde5;
  --shadow: 0 14px 28px rgba(17, 24, 39, 0.1);
}

* {
  box-sizing: border-box;
}

body {
  display: grid;
  grid-template-rows: auto minmax(0, 1fr);
  margin: 0;
  min-height: 100vh;
  height: 100vh;
  overflow: hidden;
  font-family: "Bahnschrift", "Segoe UI", sans-serif;
  color: var(--ink);
  background:
    radial-gradient(circle at top left, rgba(214, 35, 40, 0.12), transparent 28%),
    linear-gradient(180deg, #fdfcf9 0%, #eef2f7 100%);
}

button,
input,
select,
textarea {
  font: inherit;
}

button {
  border: 1px solid #bcc5d4;
  border-radius: 12px;
  background: #fff;
  color: var(--ink);
  cursor: pointer;
  transition: background 140ms ease, border-color 140ms ease, box-shadow 140ms ease, transform 140ms ease;
}

button:hover,
.file-btn:hover {
  background: #f8fafc;
  border-color: #97a7bf;
}

button:active,
.file-btn:active {
  transform: translateY(1px);
}

.primary-btn,
.file-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: 11px 16px;
  border-radius: 12px;
  border: 1px solid var(--cv-red);
  background: var(--cv-red);
  color: #fff;
  font-weight: 800;
}

.ghost-btn,
.action-row button,
.tool-btn {
  padding: 7px 10px;
  font-weight: 700;
  font-size: 0.78rem;
  letter-spacing: 0.02em;
}

.danger-btn {
  border-color: #d62328;
  color: #d62328;
}

.app-header {
  position: sticky;
  top: 0;
  z-index: 20;
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
  padding: 8px 12px;
  border-bottom: 1px solid rgba(35, 48, 71, 0.08);
  background: rgba(255, 255, 255, 0.86);
  backdrop-filter: blur(10px);
}

.header-title {
  display: grid;
  gap: 0;
}

.app-header h1 {
  margin: 0;
  font-size: 1rem;
}

.header-workspace {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  flex-wrap: wrap;
  gap: 8px;
  min-width: min(760px, 100%);
}

.header-fields {
  display: grid;
  grid-template-columns: minmax(220px, 280px);
  gap: 6px;
}

.header-customer {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 8px;
  font-size: 0.8rem;
  padding: 0 2px;
}

.header-project-link {
  color: var(--ink-soft);
}

.header-actions {
  display: flex;
  align-items: center;
  gap: 6px;
}

.header-menu {
  position: relative;
}

.header-menu summary {
  list-style: none;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-height: 36px;
  padding: 7px 12px;
  border: 1px solid #bcc5d4;
  border-radius: 12px;
  background: #fff;
  font-size: 0.78rem;
  font-weight: 700;
  cursor: pointer;
}

.header-menu summary::-webkit-details-marker {
  display: none;
}

.header-menu[open] summary {
  border-color: var(--cv-red);
  color: var(--cv-red);
}

.header-menu-panel {
  position: absolute;
  top: calc(100% + 8px);
  right: 0;
  z-index: 10;
  display: grid;
  gap: 6px;
  min-width: 150px;
  padding: 8px;
  border: 1px solid rgba(35, 48, 71, 0.12);
  border-radius: 14px;
  background: rgba(255, 255, 255, 0.98);
  box-shadow: 0 16px 40px rgba(17, 24, 39, 0.18);
}

.header-menu-panel button,
.header-menu-panel .file-btn {
  width: 100%;
}

.header-menu-panel .file-btn {
  border: 1px solid #bcc5d4;
  background: #fff;
  color: var(--ink);
  font-weight: 700;
}

.layout {
  display: grid;
  grid-template-columns: 236px minmax(0, 1fr);
  gap: 8px;
  padding: 8px;
  min-height: 0;
  height: 100%;
  overflow: hidden;
}

.toolbar-panel {
  display: grid;
  grid-template-rows: minmax(0, 1fr) auto;
  gap: 10px;
  min-height: 0;
  overflow: hidden;
  padding-right: 4px;
  align-self: stretch;
}

.tool-panel-card {
  min-height: 0;
  overflow: auto;
}

.panel-card {
  background: var(--panel);
  border: 1px solid rgba(35, 48, 71, 0.09);
  border-radius: 14px;
  box-shadow: var(--shadow);
  padding: 10px;
}

.summary-card {
  position: sticky;
  bottom: 0;
  z-index: 2;
  align-self: end;
}

.panel-heading {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 6px;
  margin-bottom: 8px;
}

.panel-heading h2 {
  margin: 0;
  font-size: 0.92rem;
}

.segmented {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.tool-strip {
  justify-content: flex-end;
}

.tool-sections {
  display: grid;
  gap: 8px;
}

.tool-section[hidden] {
  display: none !important;
}

.tool-subtitle {
  margin-top: 4px;
  font-size: 0.76rem;
}

.compact-grid {
  gap: 8px;
}

.checkbox-span,
.tool-actions-span {
  grid-column: 1 / -1;
}

.tool-btn.active {
  border-color: var(--cv-red);
  background: rgba(214, 35, 40, 0.1);
  color: var(--cv-red);
  box-shadow: 0 8px 18px rgba(214, 35, 40, 0.12);
}

.tool-btn.continuous-active {
  border-color: #2563eb;
  background: rgba(37, 99, 235, 0.1);
  color: #2563eb;
  box-shadow: 0 8px 18px rgba(37, 99, 235, 0.12);
  animation: pulse 2s infinite;
}

@keyframes pulse {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.7; }
}

.control-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 8px;
}

label {
  display: grid;
  gap: 4px;
  font-size: 0.78rem;
  font-weight: 700;
  color: var(--ink);
}

.checkbox-label {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 0.78rem;
  font-weight: 700;
  color: var(--ink);
  margin: 0;
}

.checkbox-label input[type="checkbox"] {
  width: 16px;
  height: 16px;
  margin: 0;
}

input[type="text"],
input[type="number"],
select,
textarea {
  width: 100%;
  border: 1px solid #c9d2de;
  border-radius: 10px;
  padding: 7px 9px;
  background: #fbfcfe;
  color: var(--ink);
}

input[type="range"] {
  width: 100%;
}

textarea {
  resize: vertical;
}

.action-row {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin-top: 6px;
}

.action-row.compact {
  margin-top: 6px;
}

.page-nav {
  display: flex;
  align-items: center;
  gap: 10px;
}

.hint {
  margin: 0;
  color: var(--ink-soft);
}

.pill {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 56px;
  padding: 4px 8px;
  border-radius: 999px;
  background: #eef3fb;
  color: #3e5577;
  font-size: 0.8rem;
  font-weight: 800;
}

.empty-state {
  padding: 10px;
  border-radius: 12px;
  background: #f8fafc;
  color: var(--ink-soft);
  font-size: 0.8rem;
}

.editor-block {
  display: grid;
  gap: 8px;
}

.editor-subsection {
  display: grid;
  gap: 8px;
  padding-top: 8px;
  border-top: 1px solid rgba(35, 48, 71, 0.08);
}

.collapsible-section {
  border: 1px solid rgba(35, 48, 71, 0.08);
  border-radius: 14px;
  background: rgba(248, 250, 252, 0.72);
}

.collapsible-section > summary {
  cursor: pointer;
  list-style: none;
  padding: 10px 12px;
  font-weight: 700;
  font-size: 0.84rem;
  color: #22324c;
}

.collapsible-section > summary::-webkit-details-marker {
  display: none;
}

.collapsible-section > summary::after {
  content: "+";
  float: right;
  color: #6b7280;
  font-size: 0.95rem;
}

.collapsible-section[open] > summary::after {
  content: "-";
}

.collapsible-body {
  display: grid;
  gap: 8px;
  padding: 0 12px 12px;
}

.nested-collapse {
  background: #ffffff;
}

.compact-heading {
  margin-bottom: 0;
}

.compact-heading h3 {
  margin: 0;
  font-size: 0.82rem;
}

.openings-list {
  display: grid;
  gap: 8px;
}

.opening-row {
  display: grid;
  gap: 8px;
  padding: 8px;
  border-radius: 12px;
  background: #f8fafc;
  border: 1px solid rgba(35, 48, 71, 0.08);
}

.opening-row.is-active {
  border-color: rgba(214, 35, 40, 0.45);
  box-shadow: inset 0 0 0 1px rgba(214, 35, 40, 0.18);
  background: #fff7f7;
}

.summary-list {
  display: grid;
  gap: 8px;
}

#cabinetEditor,
#wallOpeningsPanel {
  margin-top: 4px;
  padding-top: 8px;
  border-top: 1px solid rgba(35, 48, 71, 0.08);
}

.summary-item {
  display: flex;
  justify-content: space-between;
  gap: 8px;
  padding: 8px 10px;
  border-radius: 12px;
  background: #f8fafc;
  font-size: 0.8rem;
}

.summary-item strong {
  max-width: 180px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  text-align: right;
}

.history-card {
  display: grid;
  gap: 6px;
  padding: 8px 10px;
  border-radius: 12px;
  background: #f8fafc;
}

.history-card strong {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.history-card .muted {
  color: var(--ink-soft);
  font-size: 0.84rem;
}

.history-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.canvas-panel {
  min-width: 0;
  display: flex;
  flex-direction: column;
  gap: 10px;
  min-height: 0;
}

.canvas-topbar {
  position: sticky;
  top: 0;
  z-index: 5;
  display: grid;
  gap: 6px;
  padding-bottom: 4px;
  background: linear-gradient(180deg, rgba(246, 249, 252, 0.96), rgba(246, 249, 252, 0.78));
  backdrop-filter: blur(8px);
}

.canvas-toolstrip {
  flex-wrap: wrap;
  gap: 6px;
  padding: 2px 2px 0;
}

.page-tabs {
  display: flex;
  align-items: center;
  gap: 6px;
  overflow-x: auto;
  padding: 6px 10px 0;
  border-bottom: 1px solid rgba(35, 48, 71, 0.14);
}

.page-tab {
  flex: 0 0 auto;
  min-height: 34px;
  padding: 7px 14px 8px;
  border-radius: 12px 12px 0 0;
  border: 1px solid rgba(35, 48, 71, 0.14);
  border-bottom: none;
  background: linear-gradient(180deg, rgba(243, 246, 251, 0.98), rgba(231, 237, 245, 0.98));
  font-size: 0.8rem;
  font-weight: 700;
  white-space: nowrap;
  position: relative;
  top: 1px;
}

.page-tab.active {
  border-color: var(--cv-red);
  background: linear-gradient(180deg, #ffffff 0%, #fffaf9 100%);
  color: var(--cv-red);
  box-shadow: inset 0 3px 0 rgba(214, 35, 40, 0.9);
  z-index: 1;
}

.page-tab-add {
  min-width: 34px;
  padding-inline: 0;
  font-size: 1rem;
  background: linear-gradient(180deg, rgba(255,255,255,0.9), rgba(239,244,249,0.9));
}

.canvas-viewport {
  flex: 1;
  min-height: 0;
  overflow: auto;
  border-radius: 18px;
  border: 1px solid rgba(35, 48, 71, 0.09);
  background:
    linear-gradient(180deg, rgba(255,255,255,0.72), rgba(255,255,255,0.92)),
    repeating-linear-gradient(0deg, transparent, transparent 29px, rgba(35, 48, 71, 0.04) 29px, rgba(35, 48, 71, 0.04) 30px),
    repeating-linear-gradient(90deg, transparent, transparent 29px, rgba(35, 48, 71, 0.04) 29px, rgba(35, 48, 71, 0.04) 30px);
  box-shadow: var(--shadow);
  padding: 12px;
  cursor: grab;
  overscroll-behavior: contain;
  touch-action: none;
}

.canvas-viewport.pan-ready {
  cursor: grab;
}

.canvas-viewport.is-panning {
  cursor: grabbing;
}

#drawingCanvas {
  display: block;
  background: var(--paper);
  border-radius: 14px;
  box-shadow: 0 16px 32px rgba(35, 48, 71, 0.16);
  touch-action: none;
}

.inspector-heading {
  margin-top: 10px;
  padding-top: 10px;
  border-top: 1px solid rgba(35, 48, 71, 0.08);
}

.context-menu {
  position: fixed;
  z-index: 50;
  min-width: 220px;
  padding: 10px;
  border-radius: 14px;
  background: rgba(255, 255, 255, 0.98);
  border: 1px solid rgba(35, 48, 71, 0.12);
  box-shadow: 0 16px 40px rgba(17, 24, 39, 0.18);
}

.context-menu-content {
  display: grid;
  gap: 8px;
}

.context-menu-header {
  font-size: 0.74rem;
  font-weight: 700;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  color: #6b7280;
}

.context-menu button {
  text-align: left;
}

.context-menu button.danger-btn {
  color: #b91c1c;
}

.context-menu[hidden] {
  display: none !important;
}

@media (max-width: 1100px) {
  .layout {
    grid-template-columns: 1fr;
    height: auto;
  }

  .tool-panel-card {
    position: static;
  }

  .toolbar-panel {
    overflow: visible;
    padding-right: 0;
    max-height: 48vh;
  }

  .tool-panel-card {
    max-height: 100%;
  }

  .canvas-panel {
    min-height: 70vh;
  }

  .control-grid {
    grid-template-columns: 1fr;
  }
}

@media (max-width: 900px) {
  .summary-card {
    display: none;
  }

  .toolbar-panel {
    max-height: 42vh;
  }

  .context-menu {
    min-width: calc(100% - 24px);
    left: 12px !important;
    right: 12px;
    top: auto !important;
    bottom: 12px;
    position: fixed;
  }

  .context-menu button {
    min-height: 46px;
    padding: 12px 14px;
    font-size: 1rem;
  }

  .panel-card {
    padding: 8px;
  }

  .panel-heading h2 {
    font-size: 0.9rem;
  }

  .tool-section {
    padding: 0;
  }
}

.compact-hint {
  margin-bottom: 10px;
  font-size: 0.78rem;
  line-height: 1.35;
}

.toolbar-panel button,
.toolbar-panel .file-btn {
  min-height: 34px;
}

@media (max-width: 1200px) {
  .layout {
    grid-template-columns: 220px minmax(0, 1fr);
  }
}

@media (max-width: 900px) {
  .app-header {
    flex-direction: column;
  }

  .header-fields {
    grid-template-columns: 1fr 1fr;
  }
}
