/* ── Reset & base ────────────────────────────────────────────────────────── */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

/* ── Theme tokens ────────────────────────────────────────────────────────── */
:root {
  /* Brand accents */
  --accent:       #ff7a59;
  --accent-hover: #e86747;
  --blue:         #0091ae;
  --blue-hover:   #007a94;
  --red:          #e35a35;
  --green:        #00bda5;
  --radius:       6px;

  /* Light-mode surface tokens */
  --bg:         #f5f8fa;
  --surface:    #ffffff;
  --surface-alt:#f0f4f8;
  --text:       #1d2e3f;
  --text-muted: #516f90;
  --border:     #dde3eb;
  --header-bg:  #1d2e3f;
  --input-bg:   #f5f8fa;
  --input-text: #1d2e3f;
  --shadow:     0 2px 8px rgba(0,0,0,.09);
}

[data-theme="dark"] {
  --bg:         #12161f;
  --surface:    #1c2233;
  --surface-alt:#222840;
  --text:       #dce4ed;
  --text-muted: #7b90a8;
  --border:     #2e384d;
  --header-bg:  #0d1017;
  --input-bg:   #222840;
  --input-text: #dce4ed;
  --shadow:     0 2px 12px rgba(0,0,0,.35);
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg:         #12161f;
    --surface:    #1c2233;
    --surface-alt:#222840;
    --text:       #dce4ed;
    --text-muted: #7b90a8;
    --border:     #2e384d;
    --header-bg:  #0d1017;
    --input-bg:   #222840;
    --input-text: #dce4ed;
    --shadow:     0 2px 12px rgba(0,0,0,.35);
  }
}

/* ── Base ────────────────────────────────────────────────────────────────── */
body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  font-size: 15px;
  background: var(--bg);
  color: var(--text);
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  transition: background .2s, color .2s;
}

/* ── Header ──────────────────────────────────────────────────────────────── */
.app-header {
  background: var(--header-bg);
  color: #fff;
  padding: 0 1.5rem;
  height: 56px;
  display: flex;
  align-items: center;
  box-shadow: var(--shadow);
  position: sticky;
  top: 0;
  z-index: 10;
}

.header-inner {
  display: flex;
  align-items: center;
  gap: 1rem;
  width: 100%;
}

.app-header h1 {
  font-size: 1.1rem;
  font-weight: 600;
  letter-spacing: .01em;
  white-space: nowrap;
}

.user-info {
  margin-left: auto;
  display: flex;
  align-items: center;
  gap: .75rem;
  font-size: .85rem;
}

.user-info #user-name { opacity: .8; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 160px; }

.btn-signout {
  color: #fff;
  opacity: .6;
  text-decoration: none;
  white-space: nowrap;
  transition: opacity .15s;
}
.btn-signout:hover { opacity: 1; }

.btn-theme-toggle {
  background: transparent;
  border: 1px solid rgba(255,255,255,.25);
  color: #fff;
  border-radius: var(--radius);
  padding: .28rem .55rem;
  font-size: 1rem;
  line-height: 1;
  cursor: pointer;
  transition: background .15s;
  flex-shrink: 0;
}
.btn-theme-toggle:hover { background: rgba(255,255,255,.12); }

/* ── Shared bar + combobox (deal bar & product bar) ──────────────────────── */
.deal-bar,
.product-bar {
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  padding: .6rem 1.5rem;
  transition: background .2s;
}

.bar-inner {
  max-width: 960px;
  margin: 0 auto;
  display: flex;
  align-items: center;
  gap: 1rem;
}

.bar-label {
  font-size: .75rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: .06em;
  color: var(--text-muted);
  white-space: nowrap;
}

.deal-combobox,
.product-combobox {
  position: relative;
  flex: 1;
}

.bar-search-input {
  width: 100%;
  padding: .5rem .75rem;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  font-size: .9rem;
  color: var(--input-text);
  background: var(--input-bg);
  outline: none;
  transition: border-color .15s, background .15s;
}

.bar-search-input:focus {
  border-color: var(--blue);
  background: var(--surface);
}

.bar-search-input.has-value {
  background: var(--surface);
  font-weight: 500;
}

.bar-dropdown {
  position: absolute;
  top: calc(100% + 4px);
  left: 0;
  right: 0;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  z-index: 100;
  max-height: 320px;
  overflow-y: auto;
  list-style: none;
}

/* Deal dropdown — simple text items */
.bar-dropdown li.deal-option {
  padding: .55rem .85rem;
  font-size: .9rem;
  color: var(--text);
  cursor: pointer;
  border-bottom: 1px solid var(--border);
  transition: background .1s;
}
.bar-dropdown li.deal-option:last-child { border-bottom: none; }
.bar-dropdown li.deal-option:hover { background: var(--surface-alt); }
.bar-dropdown li.deal-option.selected { background: rgba(0,145,174,.08); font-weight: 600; }

/* Product dropdown — richer items with price + add button */
.product-dropdown-item {
  display: flex;
  align-items: center;
  gap: .75rem;
  padding: .55rem .85rem;
  border-bottom: 1px solid var(--border);
  transition: background .1s;
}
.product-dropdown-item:last-child { border-bottom: none; }
.product-dropdown-item:hover { background: var(--surface-alt); }

.pdi-info { flex: 1; min-width: 0; }
.pdi-name {
  display: block;
  font-size: .9rem;
  font-weight: 500;
  color: var(--text);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.pdi-sku {
  display: block;
  font-size: .76rem;
  color: var(--text-muted);
}
.pdi-price {
  font-size: .9rem;
  font-weight: 600;
  color: var(--text);
  white-space: nowrap;
}

.bar-dropdown-empty {
  padding: .75rem;
  color: var(--text-muted);
  font-size: .85rem;
  text-align: center;
}

/* ── Layout ──────────────────────────────────────────────────────────────── */
.app-main {
  padding: 1.25rem;
  flex: 1;
  max-width: 960px;
  margin: 0 auto;
  width: 100%;
}

/* ── Panel ───────────────────────────────────────────────────────────────── */
.panel {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  overflow: hidden;
  transition: background .2s, border-color .2s;
}

.panel h2 {
  font-size: .9rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: .06em;
  color: var(--text-muted);
  padding: .9rem 1.1rem;
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  gap: .6rem;
}

/* ── Cart ────────────────────────────────────────────────────────────────── */
.panel-cart { display: flex; flex-direction: column; }

.cart-count {
  font-size: .75rem;
  font-weight: 600;
  background: var(--blue);
  color: #fff;
  border-radius: 12px;
  padding: .15rem .55rem;
  text-transform: none;
  letter-spacing: 0;
}

.cart-empty {
  padding: 2.5rem 1.5rem;
  text-align: center;
  color: var(--text-muted);
  font-size: .9rem;
  line-height: 1.5;
}

/* Horizontal scroll wrapper — prevents table overflow on narrow screens */
.cart-table-wrap {
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
}

.cart-table {
  width: 100%;
  border-collapse: collapse;
  font-size: .9rem;
}

.cart-table thead th {
  background: var(--surface-alt);
  border-bottom: 1px solid var(--border);
  padding: .6rem 1rem;
  text-align: left;
  font-size: .78rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: .05em;
  color: var(--text-muted);
  white-space: nowrap;
}

.cart-table tbody tr { border-bottom: 1px solid var(--border); }
.cart-table tbody tr:last-child { border-bottom: none; }

.cart-table td {
  padding: .6rem 1rem;
  vertical-align: middle;
}

/* Existing (read-only) rows */
.existing-row { background: var(--surface-alt); }
.existing-row .cart-product-name { color: var(--text-muted); }
.existing-row .existing-val { color: var(--text-muted); font-size: .9rem; }
.existing-row .existing-total { color: var(--text-muted); font-weight: 600; white-space: nowrap; }

.cart-product-name { font-weight: 500; }

/* Cart inputs */
.desc-input {
  width: 100%;
  min-width: 120px;
  padding: .35rem .5rem;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  font-size: .9rem;
  color: var(--input-text);
  background: var(--input-bg);
  outline: none;
}
.desc-input:focus { border-color: var(--blue); }

.qty-input, .price-input {
  padding: .35rem .5rem;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  font-size: .9rem;
  text-align: right;
  color: var(--input-text);
  background: var(--input-bg);
  outline: none;
}
.qty-input   { width: 60px; }
.price-input { width: 80px; }
.qty-input:focus, .price-input:focus { border-color: var(--blue); }

.cart-line-total { font-weight: 600; white-space: nowrap; }

.cart-table tfoot td {
  padding: .75rem 1rem;
  border-top: 2px solid var(--border);
}

.total-label {
  text-align: right;
  font-weight: 700;
  font-size: .85rem;
  text-transform: uppercase;
  letter-spacing: .05em;
  color: var(--text-muted);
}

.order-total {
  font-size: 1.1rem;
  font-weight: 700;
  color: var(--text);
  white-space: nowrap;
}

/* ── Cart actions ────────────────────────────────────────────────────────── */
.cart-actions {
  padding: 1rem;
  border-top: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  gap: .6rem;
}

.submit-msg { font-size: .85rem; text-align: center; }
.submit-msg.success { color: var(--green); }
.submit-msg.error   { color: var(--red); }

/* ── Buttons ─────────────────────────────────────────────────────────────── */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: .45rem .9rem;
  border: none;
  border-radius: var(--radius);
  font-size: .85rem;
  font-weight: 600;
  cursor: pointer;
  transition: background .15s, opacity .15s;
}
.btn:disabled { opacity: .45; cursor: not-allowed; }

.btn-add {
  background: var(--blue);
  color: #fff;
  white-space: nowrap;
  flex-shrink: 0;
}
.btn-add:hover:not(:disabled) { background: var(--blue-hover); }

.btn-remove {
  background: transparent;
  color: var(--text-muted);
  font-size: .95rem;
  padding: .3rem .5rem;
}
.btn-remove:hover { color: var(--red); }

.btn-primary {
  background: var(--accent);
  color: #fff;
  width: 100%;
  padding: .7rem;
  font-size: .95rem;
}
.btn-primary:hover:not(:disabled) { background: var(--accent-hover); }

/* ── Toast notification ──────────────────────────────────────────────────── */
.toast {
  position: fixed;
  bottom: 1.25rem;
  left: 50%;
  transform: translateX(-50%);
  background: var(--green);
  color: #fff;
  font-size: .875rem;
  font-weight: 600;
  padding: .6rem 1.25rem;
  border-radius: 999px;
  box-shadow: 0 4px 16px rgba(0,0,0,.18);
  z-index: 200;
  white-space: nowrap;
  opacity: 1;
  transition: opacity .3s ease;
  pointer-events: none;
}
.toast.fade-out { opacity: 0; }

/* Product row flash on add */
@keyframes flash-add {
  0%   { background: transparent; }
  30%  { background: rgba(0, 189, 165, 0.22); }
  100% { background: transparent; }
}
.flash-add { animation: flash-add 0.5s ease forwards; }

/* ── Utility ─────────────────────────────────────────────────────────────── */
.hidden { display: none !important; }

.state-msg {
  text-align: center;
  color: var(--text-muted);
  padding: 2rem 1rem;
  font-size: .9rem;
}

/* ── Mobile improvements ─────────────────────────────────────────────────── */

/* Prevent iOS from zooming into inputs */
@media (max-width: 600px) {
  .bar-search-input,
  .desc-input,
  .qty-input,
  .price-input { font-size: 16px; }
}

/* Better touch targets and compact layout on small screens */
@media (max-width: 600px) {
  .app-header { padding: 0 1rem; }
  .app-header h1 { font-size: 1rem; }

  .deal-bar, .product-bar { padding: .5rem 1rem; }
  .bar-label { display: none; } /* give full width to the search input */
  .bar-inner { gap: 0; }

  .app-main { padding: .75rem; }

  .btn-add  { min-height: 40px; }
  .btn-remove { min-height: 40px; min-width: 40px; }

  .cart-table td, .cart-table th { padding: .45rem .6rem; }
  .qty-input   { width: 52px; }
  .price-input { width: 68px; }
}

/* ── Estimate section ────────────────────────────────────────────────────── */

.panel-estimate {
  margin-top: 1.25rem;
}

.estimate-form { display: flex; flex-direction: column; gap: .875rem; padding: 1rem 1.1rem; }

.est-form-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: .75rem;
}

.est-label {
  display: flex;
  flex-direction: column;
  gap: .3rem;
  font-size: .8rem;
  font-weight: 600;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: .4px;
}

.est-label-full { grid-column: 1 / -1; }

.est-input {
  padding: .5rem .7rem;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--input-bg);
  color: var(--input-text);
  font-size: .95rem;
  font-family: inherit;
  transition: border-color .15s, box-shadow .15s;
}
.est-input:focus {
  outline: none;
  border-color: var(--blue);
  box-shadow: 0 0 0 3px rgba(0, 145, 174, .15);
  background: var(--surface);
}

.est-textarea {
  padding: .5rem .7rem;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--input-bg);
  color: var(--input-text);
  font-size: .95rem;
  font-family: inherit;
  resize: vertical;
  transition: border-color .15s, box-shadow .15s;
}
.est-textarea:focus {
  outline: none;
  border-color: var(--blue);
  box-shadow: 0 0 0 3px rgba(0, 145, 174, .15);
  background: var(--surface);
}

.est-actions {
  display: flex;
  gap: .75rem;
  flex-wrap: wrap;
}

/* Secondary button variant */
.btn-secondary {
  background: var(--surface-alt);
  color: var(--text);
  border: 1px solid var(--border);
  padding: .55rem 1.1rem;
  border-radius: var(--radius);
  font-size: .9rem;
  font-weight: 600;
  cursor: pointer;
  transition: background .15s, box-shadow .15s;
}
.btn-secondary:hover { background: var(--border); box-shadow: var(--shadow); }
.btn-secondary:disabled { opacity: .45; cursor: not-allowed; }

.btn-sm {
  padding: .3rem .75rem;
  font-size: .8rem;
}

.btn-danger {
  background: transparent;
  color: var(--red);
  border: 1px solid var(--red);
  border-radius: var(--radius);
  font-weight: 600;
  cursor: pointer;
  transition: background .15s, color .15s;
}
.btn-danger:hover { background: var(--red); color: #fff; }

/* Status badges */
.est-badge {
  display: inline-block;
  padding: .2rem .6rem;
  border-radius: 999px;
  font-size: .75rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: .4px;
}
.badge-sent   { background: rgba(0, 145, 174, .15); color: var(--blue); }
.badge-viewed { background: rgba(255, 174, 31, .15); color: #c98a00; }
.badge-signed { background: rgba(0, 189, 165, .15);  color: var(--green); }

[data-theme="dark"] .badge-viewed { color: #f5c842; }

/* Estimate history */
.estimate-history { margin-top: 1.5rem; padding-top: 1.25rem; border-top: 1px solid var(--border); }
.est-history-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: .875rem;
}
.est-history-title {
  font-size: .85rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: .5px;
  color: var(--text-muted);
}
.estimate-history-list { display: flex; flex-direction: column; gap: .6rem; }

.est-history-row {
  display: flex;
  align-items: center;
  gap: 1rem;
  padding: .65rem .875rem;
  background: var(--surface-alt);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  flex-wrap: wrap;
}
.est-history-meta {
  display: flex;
  align-items: center;
  gap: .5rem;
  flex: 1;
  min-width: 160px;
}
.est-history-number { font-weight: 700; font-size: .9rem; }
.est-history-dates {
  font-size: .8rem;
  color: var(--text-muted);
  display: flex;
  gap: .35rem;
  flex-wrap: wrap;
}
.est-history-sep { opacity: .4; }
.est-history-actions { display: flex; gap: .5rem; align-items: center; flex-wrap: wrap; }

@media (max-width: 600px) {
  .est-form-grid { grid-template-columns: 1fr; }
  .est-input, .est-textarea { font-size: 16px; }
  .est-history-row { flex-direction: column; align-items: flex-start; gap: .5rem; }
}

/* ── Footer ──────────────────────────────────────────────────────────────── */
.app-footer {
  text-align: center;
  padding: 1.25rem 1rem 1.75rem;
  font-size: .78rem;
  color: var(--text-muted);
}

.app-footer a {
  color: var(--text-muted);
  text-decoration: underline;
  text-underline-offset: 2px;
  transition: color .15s;
}
.app-footer a:hover { color: var(--text); }

/* Date column — compact, no-wrap */
.cart-date-cell { white-space: nowrap; font-size: .85rem; color: var(--text-muted); }
