:root {
  --bg: #ffffff;
  --surface: #ffffff;
  --surface-2: #f7f9fc;
  --surface-3: #e8edf5;
  --ink: #17202c;
  --muted: #5e6878;
  --accent: #52647e;
  --accent-2: #3e4e66;
  --line: #ced7e5;
  --line-strong: #aeb9ca;
  --success: #1d7f48;
  --warn: #9a6400;
  --focus: #2f6fce;
  --shadow: rgba(27, 39, 56, 0.14);
  --overlay: #ffffff;
  --calc-overlay: rgba(255, 255, 255, 0.92);
  --button-bg: linear-gradient(180deg, #65758f, #45546c);
  --secondary-bg: #f7f9fc;
  --table-head: #e7edf5;
  --input-focus-bg: #eaf1fb;
  --drop-bg: #f8fafc;
  --drop-active-bg: #edf4ff;
  --link: #2f6fce;
}

:root[data-theme="dark"] {
  --bg: #0f1217;
  --surface: #171b22;
  --surface-2: #202631;
  --surface-3: #252c38;
  --ink: #f4f6f8;
  --muted: #a9b2bf;
  --accent: #7d8ca3;
  --accent-2: #5d6a7c;
  --line: #343d4d;
  --line-strong: #4b5669;
  --success: #89d6a3;
  --warn: #f3c46d;
  --focus: #9fc5ff;
  --shadow: rgba(0, 0, 0, 0.28);
  --overlay: rgba(8, 10, 13, 0.72);
  --calc-overlay: rgba(23, 27, 34, 0.82);
  --button-bg: linear-gradient(180deg, var(--accent), var(--accent-2));
  --secondary-bg: #222936;
  --table-head: #1f2630;
  --input-focus-bg: #293241;
  --drop-bg: #1d232d;
  --drop-active-bg: #263141;
  --link: #b8d2ff;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  font-family: "Segoe UI", Tahoma, sans-serif;
  color: var(--ink);
  background: var(--bg);
}

body.results-open {
  overflow: hidden;
}

.page {
  width: min(1220px, 100%);
  margin: 0 auto;
  padding: 0 18px 28px;
  height: 100vh;
  overflow: hidden;
}

.admin-page {
  width: min(1400px, 100%);
  height: auto;
  min-height: 100vh;
  overflow: visible;
}

.hidden {
  display: none !important;
}

.login-required-overlay {
  position: fixed;
  inset: 0;
  z-index: 1200;
  display: grid;
  place-items: start center;
  padding: 60px 18px 18px;
  background: rgba(255, 255, 255, 0.94);
}

.login-required-card {
  width: min(520px, 100%);
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface);
  box-shadow: 0 18px 45px var(--shadow);
  padding: 20px;
  text-align: center;
}

.login-required-card p {
  margin: 8px 0 16px;
  color: var(--muted);
}

.login-required-card button {
  min-width: 140px;
}

:root[data-theme="dark"] .login-required-overlay {
  background: rgba(15, 18, 23, 0.9);
}

h1,
h2 {
  margin: 0;
  letter-spacing: 0;
}

h1 {
  font-size: 34px;
  line-height: 1.08;
}

h2 {
  font-size: 18px;
}

.sub {
  margin: 5px 0 0;
  color: var(--muted);
}

.quote-workspace {
  height: min(675px, calc(100vh - 56px));
  max-height: calc(100vh - 56px);
  display: block;
  min-height: 0;
}

.quote-meta {
  display: flex;
  flex-wrap: wrap;
  justify-content: flex-end;
  gap: 8px;
}

.quote-meta span,
#timerBadge {
  color: var(--muted);
  border: 1px solid var(--line);
  border-radius: 999px;
  padding: 5px 10px;
  font-size: 12px;
  white-space: nowrap;
}

.entry-shell {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 310px;
  gap: 16px;
  align-items: stretch;
  height: 100%;
  min-height: 0;
}

.entry-main,
.card,
.upload-panel,
.modal-card,
.results-panel {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 8px;
  box-shadow: 0 18px 45px var(--shadow);
}

.entry-main {
  min-height: 0;
  display: grid;
  grid-template-rows: auto 1fr auto;
  padding: 16px;
  overflow: hidden;
  position: relative;
}

.card {
  padding: 16px;
}

.entry-title-row {
  display: grid;
  grid-template-columns: minmax(190px, 1fr) auto auto;
  align-items: center;
  gap: 14px;
  margin-bottom: 14px;
}

.entry-actions,
.results-actions,
.actions {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 8px;
}

.entry-footer {
  min-height: 36px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding-top: 12px;
}

.calc-overlay {
  position: absolute;
  inset: 0;
  z-index: 20;
  display: grid;
  place-items: center;
  padding: 18px;
  background: var(--calc-overlay);
  backdrop-filter: blur(3px);
}

.calc-card {
  width: min(360px, 100%);
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface);
  box-shadow: 0 18px 45px var(--shadow);
  padding: 22px;
  text-align: center;
}

.calc-card h2 {
  margin-top: 12px;
}

.calc-card p {
  min-height: 22px;
  margin: 8px 0 8px;
  color: var(--muted);
}

.calc-card span {
  display: inline-block;
  border: 1px solid var(--line);
  border-radius: 999px;
  padding: 4px 10px;
  color: var(--muted);
  font-size: 12px;
}

.calc-loader {
  width: 42px;
  height: 42px;
  margin: 0 auto;
  border-radius: 999px;
  border: 4px solid var(--line);
  border-top-color: var(--accent);
  animation: spin 0.9s linear infinite;
}

@keyframes spin {
  to {
    transform: rotate(360deg);
  }
}

#status {
  color: var(--muted);
}

.bom-grid-wrap {
  height: 100%;
  min-height: 0;
  border: 1px solid var(--line);
  border-radius: 8px;
  overflow: auto;
  background: var(--surface-2);
}

#bomGridTable {
  width: 100%;
  border-collapse: collapse;
  font-size: 14px;
}

#bomGridTable th,
#bomGridTable td {
  border-bottom: 1px solid var(--line);
  padding: 8px;
}

#bomGridTable th:nth-child(1),
#bomGridTable td:nth-child(1) {
  width: 44%;
}

#bomGridTable th:nth-child(2),
#bomGridTable td:nth-child(2) {
  width: 18%;
}

#bomGridTable th:nth-child(3),
#bomGridTable td:nth-child(3) {
  width: 38%;
}

#bomGridTable input {
  width: 100%;
  border: 1px solid transparent;
  border-radius: 5px;
  background: transparent;
  color: var(--ink);
  padding: 6px 7px;
  font-family: Consolas, monospace;
}

#bomGridTable input:focus {
  outline: none;
  border-color: var(--focus);
  background: var(--input-focus-bg);
}

.upload-panel {
  padding: 16px;
  display: grid;
  grid-template-rows: auto 1fr;
  gap: 12px;
  min-height: 0;
  overflow: hidden;
}

.excel-drop-zone {
  min-height: 0;
  height: 100%;
  border: 1px dashed #586477;
  background: var(--drop-bg);
  color: var(--muted);
  border-radius: 8px;
  padding: 14px;
  text-align: center;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
}

.excel-drop-zone strong {
  display: block;
  color: var(--ink);
  font-size: 16px;
}

.excel-drop-zone span {
  display: inline-block;
  margin-top: 4px;
  color: var(--link);
  text-decoration: underline;
}

.excel-drop-zone-content {
  line-height: 1.4;
}

.excel-drop-zone-active {
  border-color: var(--focus);
  background: var(--drop-active-bg);
}

button {
  border: 1px solid #78869a;
  background: var(--button-bg);
  color: white;
  border-radius: 8px;
  min-height: 36px;
  padding: 8px 13px;
  cursor: pointer;
  font-weight: 600;
}

.secondary-btn {
  background: var(--secondary-bg);
  border-color: var(--line-strong);
  color: var(--ink);
}

.icon-btn {
  width: 36px;
  min-height: 36px;
  padding: 0;
  font-size: 24px;
  line-height: 1;
  background: var(--secondary-bg);
  border-color: var(--line-strong);
  color: var(--ink);
}

button:focus-visible,
.excel-drop-zone:focus-visible,
input:focus-visible,
select:focus-visible {
  outline: 2px solid var(--focus);
  outline-offset: 2px;
}

button:disabled {
  opacity: 0.52;
  cursor: not-allowed;
}

.theme-toggle {
  display: inline-grid;
  grid-template-columns: 1fr 1fr;
  align-items: center;
  gap: 2px;
  min-height: 31px;
  padding: 3px;
  border-color: var(--line);
  background: var(--secondary-bg);
  color: var(--muted);
}

.theme-icon {
  display: grid;
  place-items: center;
  width: 24px;
  height: 24px;
  border-radius: 999px;
  font-size: 15px;
  line-height: 1;
}

:root[data-theme="dark"] .theme-moon,
:root:not([data-theme="dark"]) .theme-sun {
  color: white;
  background: var(--accent-2);
}

.table-wrap {
  overflow-x: auto;
}

table {
  width: 100%;
  border-collapse: collapse;
  font-size: 14px;
}

th,
td {
  border-bottom: 1px solid var(--line);
  text-align: left;
  padding: 10px 8px;
  white-space: nowrap;
}

#resultTable td:first-child,
#resultTable th:first-child {
  width: 58px;
  text-align: center;
}

.line-select {
  width: 17px;
  height: 17px;
  accent-color: var(--accent-2);
  cursor: pointer;
}

.line-select:disabled {
  cursor: not-allowed;
}

thead th {
  background: var(--table-head);
  color: var(--ink);
  position: sticky;
  top: 0;
  z-index: 1;
}

.results-overlay {
  position: fixed;
  inset: 0;
  z-index: 800;
  background: var(--overlay);
  padding: 0 18px 28px;
  overflow: auto;
}

.results-panel {
  width: min(1280px, 100%);
  height: min(675px, calc(100vh - 56px));
  margin: 0 auto;
  padding: 16px;
  overflow: hidden;
  display: grid;
  grid-template-rows: auto minmax(0, 1fr);
  position: relative;
}

.results-header {
  display: flex;
  justify-content: space-between;
  gap: 16px;
  align-items: center;
  border-bottom: 1px solid var(--line);
  padding-bottom: 14px;
  margin-bottom: 16px;
}

.eyebrow {
  margin: 0 0 4px;
  color: var(--success);
  font-size: 12px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0;
}

.results-timer {
  margin: 5px 0 0;
  color: var(--muted);
  font-size: 12px;
}

.results-layout {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 340px;
  gap: 16px;
  align-items: stretch;
  min-height: 0;
  overflow: hidden;
  padding-bottom: 18px;
}

.results-table-card {
  border: 1px solid var(--line);
  border-radius: 8px;
  overflow: hidden;
  background: var(--surface);
  height: 100%;
  max-height: calc(100% - 18px);
  min-height: 0;
}

.results-table-card .table-wrap {
  height: 100%;
  max-height: 100%;
  overflow: auto;
}

.quote-summary {
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface);
  padding: 14px;
  align-self: start;
  max-height: calc(100% - 18px);
  overflow: visible;
}

.quote-summary dl {
  display: grid;
  gap: 8px;
  margin: 12px 0 14px;
}

.quote-summary dl div {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  border-bottom: 1px solid var(--line);
  padding-bottom: 8px;
}

.quote-summary dt {
  color: var(--muted);
}

.quote-summary dd {
  margin: 0;
  text-align: right;
  font-weight: 700;
}

.checkout-form {
  display: grid;
  gap: 8px;
}

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

label {
  display: block;
  color: var(--muted);
  font-size: 12px;
  margin-bottom: 5px;
}

.checkout-form input,
.modal-grid select,
.modal-grid input {
  width: 100%;
  border-radius: 8px;
  border: 1px solid var(--line);
  padding: 8px;
  background: var(--surface-2);
  color: var(--ink);
}

.checkout-form button {
  margin-top: 6px;
  width: 100%;
}

.checkout-status {
  min-height: 20px;
  margin: 4px 0 0;
  color: var(--muted);
  font-size: 13px;
}

.modal-overlay {
  position: fixed;
  inset: 0;
  background: var(--overlay);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
  z-index: 999;
}

.modal-overlay.hidden {
  display: none;
}

.modal-card {
  width: min(1100px, 95vw);
  max-height: 90vh;
  overflow: auto;
  padding: 16px;
}

.modal-grid {
  display: grid;
  grid-template-columns: repeat(5, minmax(130px, 1fr));
  gap: 10px;
}

.modal-preview-wrap {
  margin-top: 12px;
}

@media (max-width: 900px) {
  .page {
    height: auto;
    overflow: visible;
  }

  .results-header {
    align-items: flex-start;
    flex-direction: column;
  }

  .entry-title-row {
    grid-template-columns: 1fr;
  }

  .quote-meta {
    justify-content: flex-start;
  }

  .entry-shell,
  .results-layout,
  .modal-grid {
    grid-template-columns: 1fr;
  }

  .entry-main {
    min-height: 520px;
    overflow: visible;
  }

  .quote-summary {
    position: static;
  }

  .results-panel {
    height: auto;
    min-height: calc(100vh - 28px);
    overflow: visible;
  }
}

@media (max-width: 620px) {
  .page,
  .results-overlay {
    padding: 0 14px 14px;
  }

  h1 {
    font-size: 28px;
  }

  .entry-title-row,
  .entry-footer {
    align-items: flex-start;
    flex-direction: column;
  }

  .form-row {
    grid-template-columns: 1fr;
  }
}
