:root {
  --excel-bg: #E7E6E6;
  --excel-sheet: #FFFFFF;
  --excel-grid: #D4D4D4;
  --excel-header: #F2F2F2;
  --excel-header-border: #D4D4D4;
  --excel-rowheader: #F2F2F2;
  --excel-selection: #217346;
  --excel-font: "Calibri", "Segoe UI", Inter, sans-serif;
  --bg: #f8f9fb;
  --surface: #ffffff;
  --border: #e2e6eb;
  --text: #1a1d21;
  --text-muted: #5c6574;
  --accent: #217346;
  --accent-hover: #1a5c38;
  --danger: #dc2626;
  --radius: 2px;
  --radius-sm: 2px;
  --font: var(--excel-font);
  --touch-min: 44px;
}

* {
  box-sizing: border-box;
}

html {
  -webkit-text-size-adjust: 100%;
}

body {
  margin: 0;
  font-family: var(--font);
  background: var(--excel-bg);
  color: var(--text);
  line-height: 1.5;
  min-height: 100vh;
  padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
}

.page {
  max-width: 1200px;
  margin: 0 auto;
  padding: 1rem;
  padding-bottom: 3rem;
}

@media (max-width: 900px) {
  html, body { overflow-x: hidden; }
  .page { min-width: 0; overflow-x: hidden; }
  .section { overflow-x: hidden; }
  .order-section { overflow-x: hidden; }
}

@media (min-width: 768px) {
  .page { padding: 1.5rem 2rem; }
}

/* Header */
.header {
  margin-bottom: 1.5rem;
}

.logo {
  font-size: 1.5rem;
  font-weight: 700;
  margin: 0;
  letter-spacing: -0.02em;
}

.tagline {
  font-size: 0.95rem;
  color: var(--text-muted);
  margin: 0.25rem 0 0 0;
}

/* Form */
.form {
  display: flex;
  flex-direction: column;
  gap: 1.5rem;
}

.section {
  background: var(--surface);
  border-radius: var(--radius);
  padding: 1.25rem;
  box-shadow: var(--shadow);
}

.order-section {
  background: var(--surface);
  border-radius: 2px;
}

@media (min-width: 768px) {
  .section { padding: 1.5rem; }
}

.section-title {
  font-size: 0.9rem;
  font-weight: 600;
  margin: 0 0 0.75rem 0;
  color: var(--text-muted);
}

/* Lines & Materials */
.materials-section { }
.materials-empty {
  margin: 0;
  font-size: 0.9rem;
  color: var(--text-muted);
}
.materials-table {
  width: 100%;
  max-width: 560px;
  border-collapse: collapse;
  font-size: 0.9rem;
}
.materials-table th,
.materials-table td {
  padding: 0.4rem 0.75rem;
  text-align: left;
}
.materials-table thead th {
  border-bottom: 1px solid var(--excel-grid);
  font-weight: 600;
  color: var(--text-muted);
}
.materials-dim-col {
  font-size: 0.85rem;
  color: var(--text-muted);
}
.materials-table tbody td:not(:first-child):not(:nth-child(2)),
.materials-table tfoot td:not(:first-child) {
  text-align: right;
  font-weight: 500;
}
.materials-total th,
.materials-total td {
  border-top: 2px solid var(--excel-grid);
  padding-top: 0.6rem;
  font-weight: 700;
  font-size: 1rem;
}

/* Lines & Materials + Price Summary: mobile card layout */
@media (max-width: 900px) {
  .materials-wrap,
  .price-summary-wrap {
    overflow-x: hidden;
  }

  .materials-table,
  .price-summary-table {
    display: block;
    width: 100%;
    max-width: none;
  }

  .materials-table thead,
  .price-summary-table thead {
    display: none;
  }

  .materials-table tbody,
  .price-summary-table tbody {
    display: block;
  }

  .materials-table tbody tr,
  .price-summary-table tbody tr {
    display: block;
    padding: 0.75rem 0;
    border-bottom: 1px solid var(--border);
  }

  .materials-table tbody tr:last-child,
  .price-summary-table tbody tr:last-child {
    border-bottom: none;
  }

  .materials-table tbody td,
  .price-summary-table tbody td {
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 0.5rem;
    padding: 0.35rem 0;
    border: none;
    font-size: 0.9rem;
  }

  .materials-table tbody td::before,
  .price-summary-table tbody td::before {
    content: attr(data-label);
    font-weight: 500;
    color: var(--text-muted);
    flex-shrink: 0;
  }

  .materials-table tfoot,
  .price-summary-table tfoot {
    display: block;
    margin-top: 0.5rem;
    padding-top: 0.75rem;
    border-top: 2px solid var(--excel-grid);
  }

  .materials-total,
  .price-summary-total {
    display: flex;
    flex-wrap: wrap;
    gap: 0.5rem 1.5rem;
    padding: 0.5rem 0 !important;
  }

  .materials-total th,
  .price-summary-total th {
    flex: 0 0 auto;
    padding: 0 !important;
    border: none !important;
  }

  .materials-total td,
  .price-summary-total td {
    padding: 0 !important;
    border: none !important;
  }

  .materials-total th:first-child,
  .price-summary-total th:first-child {
    width: 100%;
    font-size: 1rem;
    margin-bottom: 0.25rem;
  }

  .materials-total th:not(:first-child),
  .materials-total td,
  .price-summary-total td {
    font-size: 0.95rem;
  }

  /* Total row: show label before each value */
  .materials-total td[data-label]::before,
  .price-summary-total td[data-label]::before {
    content: attr(data-label) ": ";
    font-weight: 600;
    color: var(--text);
  }
}

/* Price summary */
.price-summary-section { }
.price-summary-empty {
  margin: 0;
  font-size: 0.9rem;
  color: var(--text-muted);
}
.price-summary-table {
  width: 100%;
  max-width: 520px;
  border-collapse: collapse;
  font-size: 0.9rem;
}
.price-summary-table th,
.price-summary-table td {
  padding: 0.4rem 0.75rem;
  text-align: left;
}
.price-summary-table thead th {
  border-bottom: 1px solid var(--excel-grid);
  font-weight: 600;
  color: var(--text-muted);
}
.price-dim-col {
  font-size: 0.85rem;
  color: var(--text-muted);
  max-width: 180px;
}
.price-summary-table tbody td:not(:first-child):not(.price-dim-col),
.price-summary-table tfoot td:not(:first-child) {
  text-align: right;
  font-weight: 600;
}
.price-summary-total th,
.price-summary-total td {
  border-top: 2px solid var(--excel-grid);
  padding-top: 0.6rem;
  font-weight: 700;
  font-size: 1rem;
}


/* Excel toolbar / name box */
.excel-toolbar {
  display: none;
  background: var(--excel-header);
  border: 1px solid var(--excel-grid);
  border-bottom: none;
  padding: 0.25rem 0.5rem;
  font-size: 0.75rem;
  color: #666;
}

@media (min-width: 901px) {
  .excel-toolbar { display: block; }
}

.excel-namebox {
  font-family: var(--excel-font);
}

/* Table - Excel-style sheet */
.order-section {
  padding: 0;
  overflow: hidden;
  border-radius: 0;
  box-shadow: 1px 1px 4px rgba(0,0,0,0.15);
  border: 1px solid var(--excel-grid);
  background: var(--excel-sheet);
}

.table-wrap {
  position: relative;
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
  overflow-y: visible;
}

.table-scroll-hint {
  display: none;
}

/* Mobile & tablet: card layout — no horizontal scroll */
@media (max-width: 900px) {
  .table-wrap {
    overflow-x: visible;
    overflow-y: visible;
  }

  .order-table {
    display: block;
    width: 100%;
  }

  .order-table thead {
    display: none;
  }

  .order-table tbody {
    display: block;
  }

  .order-table tbody tr {
    display: block;
    margin-bottom: 1rem;
    padding: 1rem;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 8px;
    box-shadow: 0 1px 3px rgba(0,0,0,0.06);
  }

  .order-table tbody tr:last-child {
    margin-bottom: 0;
  }

  .order-table tbody td {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    padding: 0.6rem 0;
    border: none;
    border-bottom: 1px solid rgba(0,0,0,0.06);
    background: transparent !important;
  }

  .order-table tbody td:last-child {
    border-bottom: none;
  }

  .order-table tbody td.col-item {
    padding-bottom: 0.75rem;
    border-bottom: 1px solid rgba(0,0,0,0.08);
    font-weight: 600;
    font-size: 0.95rem;
  }

  .order-table tbody td.col-item::before {
    content: "Window ";
  }

  .order-table tbody td.col-actions {
    justify-content: flex-end;
    padding-top: 0.75rem;
    border-top: 1px solid rgba(0,0,0,0.08);
    margin-top: 0.5rem;
  }

  .order-table tbody td.col-actions::before {
    display: none;
  }

  .order-table tbody td[data-label]:not(.col-item):not(.col-actions)::before {
    content: attr(data-label);
    font-size: 0.8rem;
    color: var(--text-muted);
    min-width: 100px;
    flex-shrink: 0;
  }

  .order-table .cell-wrap {
    flex: 1;
    min-width: 0;
    display: flex;
    align-items: center;
  }

  .order-table .cell-input {
    flex: 1;
    min-width: 60px;
  }

  .order-table .cell-input input,
  .order-table .cell-input select {
    max-width: none;
    min-width: 0;
    min-height: 44px;
    padding: 0.5rem 0.65rem;
    font-size: 16px;
    touch-action: manipulation;
    -webkit-tap-highlight-color: transparent;
  }

  /* Mobile: clean focus - box-shadow avoids layout shift, no jarring highlight */
  .order-table .cell-input input:focus,
  .order-table .cell-input select:focus {
    outline: none;
    border: 1px solid var(--excel-grid) !important;
    box-shadow: 0 0 0 2px rgba(33, 115, 70, 0.25) !important;
  }

  .order-table .copy-col {
    flex-shrink: 0;
    width: 2.25rem;
    height: 2.25rem;
    min-width: 2.25rem;
    min-height: 2.25rem;
    -webkit-tap-highlight-color: transparent;
  }

  /* Qty: ensure it works on mobile - adequate touch target */
  .order-table .col-qty .cell-input input {
    min-width: 4rem;
    max-width: 5rem;
  }
}

.order-table {
  width: max-content;
  min-width: 100%;
  border-collapse: collapse;
  font-size: 11px;
  font-family: var(--excel-font);
  border: 1px solid var(--excel-grid);
}

/* Excel-style grid */
.order-table th,
.order-table td {
  border: 1px solid var(--excel-grid);
  padding: 0.2rem 0.35rem;
  vertical-align: middle;
}

@media (min-width: 901px) {
  .order-table th,
  .order-table td { padding: 0.15rem 0.3rem; }
}

/* Excel column letters row (A, B, C...) */
.excel-col-row th {
  background: var(--excel-rowheader) !important;
  color: #333 !important;
  font-weight: 600;
  font-size: 10px;
  text-align: center;
  min-width: 1.5rem;
}

.col-corner {
  background: var(--excel-rowheader) !important;
  border-right: 1px solid var(--excel-header-border) !important;
  border-bottom: 1px solid var(--excel-header-border) !important;
  min-width: 1.5rem;
  width: 1.5rem;
}

.excel-col-letter {
  min-width: 2rem;
}

/* Excel header row (column labels) */
.excel-header-row th {
  background: var(--excel-header) !important;
  color: #333 !important;
  font-weight: 600;
  font-size: 11px;
}

/* Hide column letters on mobile */
@media (max-width: 900px) {
  .excel-col-row { display: none !important; }
}

/* Excel row numbers */
.order-table td.col-item {
  background: var(--excel-rowheader) !important;
  font-weight: 600;
  font-size: 11px;
  color: #333;
  text-align: center;
}

.order-table tbody td {
  background: var(--excel-sheet);
}

/* Hover only on devices that support it (desktop), avoids flash on mobile tap */
@media (hover: hover) {
  .order-table tbody tr:hover td {
    background: #E2EFDA;
  }
  .order-table tbody tr:hover td.col-item {
    background: var(--excel-rowheader) !important;
  }
}

/* Cell with copy */
.cell-wrap {
  display: flex;
  align-items: center;
  gap: 0.25rem;
}

.cell-input {
  flex: 1;
  min-width: 0;
}

.cell-input input,
.cell-input select {
  width: 100%;
  min-width: 0;
  padding: 0.2rem 0.35rem;
  border: 1px solid transparent;
  border-radius: 0;
  background: transparent;
  color: var(--text);
  font-size: 0.8rem;
  line-height: 1.3;
  font-family: inherit;
}

/* Desktop table: compact inputs */
@media (min-width: 901px) {
  .order-table .cell-input input,
  .order-table .cell-input select {
    padding: 0.2rem 0.3rem;
    font-size: 0.75rem;
    line-height: 1.2;
    min-height: 1.5rem;
  }

  /* Qty field: narrow, hide spinner */
  .col-qty .cell-input input {
    width: 2rem;
    max-width: 2rem;
    min-width: 2rem;
    padding: 0.2rem 0.2rem;
    text-align: center;
  }
  .col-qty input[type="number"]::-webkit-outer-spin-button,
  .col-qty input[type="number"]::-webkit-inner-spin-button {
    -webkit-appearance: none;
    margin: 0;
  }
  .col-qty input[type="number"] {
    -moz-appearance: textfield;
  }
}

.order-table .cell-input input:focus,
.order-table .cell-input select:focus {
  outline: none;
  background: var(--excel-sheet) !important;
}

/* Desktop: solid border focus */
@media (min-width: 901px) {
  .order-table .cell-input input:focus,
  .order-table .cell-input select:focus {
    border: 2px solid var(--excel-selection) !important;
    box-shadow: none;
  }
}

.order-table .cell-input input::placeholder {
  color: var(--text-muted);
}

.cell-input input[readonly] {
  background: var(--bg);
  color: var(--text-muted);
}

/* Column widths - compact to fit all on screen */
.col-item {
  width: 1.75rem;
  text-align: center;
  font-weight: 500;
  font-size: 0.8rem;
}

.col-room { min-width: 58px; }
.col-qty { min-width: 2.25rem; width: 2.25rem; max-width: 2.25rem; }
.col-product { min-width: 68px; }
.col-width, .col-height { min-width: 48px; }
.col-fabric-type { min-width: 70px; }
.col-fabric-colour { min-width: 62px; }
.col-window-type { min-width: 62px; }
.col-fitting { min-width: 62px; }
.col-allowance { min-width: 52px; }
.col-track-type { min-width: 78px; }
.col-track-colour { min-width: 62px; }
.col-track-finial { min-width: 52px; }
.col-stack { min-width: 62px; }
.col-mounting { min-width: 72px; }
.col-comments { min-width: 68px; }
.col-actions { width: 2rem; text-align: center; }

/* Copy button */
.btn--copy-col,
.copy-col {
  flex-shrink: 0;
  width: 1.35rem;
  height: 1.35rem;
  min-width: 1.35rem;
  min-height: 1.35rem;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  background: transparent;
  color: var(--text-muted);
  padding: 0;
  font-size: 0.7rem;
  line-height: 1;
  border: none;
  border-radius: var(--radius-sm);
  cursor: pointer;
  font-family: inherit;
}

.copy-col:hover:not(:disabled) {
  color: var(--excel-selection);
  background: #E2EFDA;
}

.copy-col:disabled {
  cursor: default;
  opacity: 0.35;
}

/* Extras grid */
/* Order header section */
.order-header-section {
  margin-bottom: 0;
}

.order-header-grid {
  display: grid;
  grid-template-columns: 1fr;
  gap: 1rem;
}

@media (min-width: 600px) {
  .order-header-grid {
    grid-template-columns: 1fr 1fr;
  }
}

@media (min-width: 900px) {
  .order-header-grid {
    grid-template-columns: 1fr 1fr 1fr;
  }
}

.order-header-grid label span {
  font-size: 0.75rem;
  color: var(--text-muted);
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.03em;
}

.order-header-grid input,
.order-header-grid select {
  width: 100%;
}

.price-visibility {
  font-size: 0.8rem;
  color: var(--text-muted);
}
.price-visibility span { margin-right: 0.35rem; }
.price-visibility select { padding: 0.2rem 0.4rem; font-size: 0.85rem; }

/* Store autocomplete */
.store-autocomplete {
  position: relative;
}

.store-dropdown {
  display: none;
  position: absolute;
  top: 100%;
  left: 0;
  right: 0;
  max-height: 200px;
  overflow-y: auto;
  background: var(--surface);
  border: 1px solid var(--excel-grid);
  border-top: none;
  box-shadow: 0 4px 12px rgba(0,0,0,0.1);
  z-index: 100;
}

.store-dropdown.is-visible {
  display: block;
}

.store-option {
  padding: 0.5rem 0.75rem;
  font-size: 0.9rem;
  cursor: pointer;
}

.store-option:hover,
.store-option.store-option--active {
  background: var(--excel-header);
}

.extras-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 1rem;
}

@media (min-width: 480px) {
  .extras-grid { grid-template-columns: repeat(4, 1fr); }
}

.extras-grid label {
  display: flex;
  flex-direction: column;
  gap: 0.35rem;
}

.extras-grid label span {
  font-size: 0.8rem;
  color: var(--text-muted);
}

.extras-grid select {
  padding: 0.6rem 0.75rem;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--surface);
  font-size: 0.9rem;
}

/* Buttons */
.btn {
  padding: 0.65rem 1.25rem;
  border-radius: var(--radius-sm);
  font-size: 0.9rem;
  font-weight: 500;
  cursor: pointer;
  border: none;
  font-family: inherit;
  min-height: var(--touch-min);
  display: inline-flex;
  align-items: center;
  justify-content: center;
}

/* Excel sheet bar with tabs */
.excel-sheet-bar {
  display: flex;
  align-items: center;
  background: var(--excel-rowheader);
  border: 1px solid var(--excel-grid);
  border-top: none;
  padding: 0.25rem 0.5rem;
}

.excel-add-btn {
  font-size: 0.8rem;
  padding: 0.25rem 0.5rem;
  min-height: auto;
}

.btn--add {
  background: var(--excel-header);
  color: #333;
  border: 1px solid var(--excel-grid);
  margin: 0;
}

.btn--add:hover {
  background: #e5e5e5;
}

.btn--submit {
  background: var(--excel-selection);
  color: white;
}

.btn--submit:hover {
  background: var(--accent-hover);
}

.btn--reset,
.btn--secondary {
  background: transparent;
  color: var(--text-muted);
  border: 1px solid var(--border);
}

.btn--reset:hover,
.btn--secondary:hover {
  background: var(--bg);
  color: var(--text);
}

.btn--remove,
.row-remove {
  background: transparent;
  color: var(--text-muted);
  min-width: var(--touch-min);
  min-height: var(--touch-min);
  padding: 0;
  font-size: 1.25rem;
  line-height: 1;
}

.btn--remove:hover,
.row-remove:hover {
  color: var(--danger);
}

/* Form actions */
.form-actions {
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
}

@media (min-width: 480px) {
  .form-actions {
    flex-direction: row;
  }
}

.form-actions .btn--submit {
  flex: 1;
}

/* Animated loading dots */
.btn--submit .loading-dots {
  display: inline-flex;
  gap: 2px;
  align-items: center;
}
.btn--submit .loading-dots span {
  width: 4px;
  height: 4px;
  border-radius: 50%;
  background: currentColor;
  animation: loading-bounce 0.6s ease-in-out infinite both;
}
.btn--submit .loading-dots span:nth-child(2) { animation-delay: 0.15s; }
.btn--submit .loading-dots span:nth-child(3) { animation-delay: 0.3s; }
@keyframes loading-bounce {
  0%, 80%, 100% { transform: scale(0.6); opacity: 0.5; }
  40% { transform: scale(1); opacity: 1; }
}

/* Order success / error modal */
.order-modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.4);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 9999;
  padding: 1rem;
  animation: modal-fade-in 0.2s ease-out;
}
.order-modal {
  background: white;
  border-radius: 8px;
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.2);
  max-width: 420px;
  width: 100%;
  padding: 1.75rem;
  text-align: center;
  animation: modal-scale-in 0.25s ease-out;
}
.order-modal-icon {
  width: 56px;
  height: 56px;
  margin: 0 auto 1.25rem;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 1.75rem;
}
.order-modal--success .order-modal-icon {
  background: #d4edda;
  color: #217346;
}
.order-modal--error .order-modal-icon {
  background: #f8d7da;
  color: #721c24;
}
.order-modal h3 {
  margin: 0 0 0.5rem;
  font-size: 1.25rem;
  font-weight: 600;
  color: #1a1d21;
}
.order-modal p {
  margin: 0 0 1.5rem;
  font-size: 0.95rem;
  color: #5c6574;
  line-height: 1.5;
}
.order-modal .btn {
  padding: 0.6rem 1.5rem;
  font-size: 0.95rem;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  font-weight: 500;
}
.order-modal--success .btn {
  background: #217346;
  color: white;
}
.order-modal--success .btn:hover {
  background: #1a5c38;
}
.order-modal--error .btn {
  background: #721c24;
  color: white;
}
.order-modal--error .btn:hover {
  background: #5a1520;
}
@keyframes modal-fade-in {
  from { opacity: 0; }
  to { opacity: 1; }
}
@keyframes modal-scale-in {
  from { opacity: 0; transform: scale(0.95); }
  to { opacity: 1; transform: scale(1); }
}
