/* Base layout */
:root {
  color-scheme: light dark;
  --bg: #f8fafc;
  --fg: #1e293b;
  --fg-muted: #475569;
  --surface: #ffffff;
  --surface-border: #e2e8f0;
  --accent: #2563eb;
  --accent-fg: #ffffff;
  --danger: #dc2626;
  --success: #16a34a;
  font-family: 'Inter', system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
}

body {
  margin: 0;
  min-height: 100vh;
  background: var(--bg);
  color: var(--fg);
  display: flex;
}

body[data-session-state="loading"] #appShell {
  visibility: hidden;
  pointer-events: none;
}

body[data-session-state="ready"] #appLoadingState {
  display: none;
}

.app-loading {
  position: fixed;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--bg);
  z-index: 1200;
}

.app-loading__content {
  text-align: center;
  color: var(--fg);
  font-weight: 600;
  display: flex;
  flex-direction: column;
  gap: 12px;
  align-items: center;
}

.app-loading__spinner {
  width: 48px;
  height: 48px;
  border-radius: 50%;
  border: 4px solid rgba(37, 99, 235, 0.2);
  border-top-color: var(--accent);
  animation: spin 0.9s linear infinite;
}

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

.app-shell {
  flex: 1;
  display: flex;
}

.quotes-table tr[data-quote-id] {
  cursor: pointer;
  transition: background 0.15s ease;
}

.quotes-table tr[data-quote-id]:hover {
  background: rgba(37, 99, 235, 0.08);
}

.quotes-table tr.quote-row--expanded {
  background: rgba(37, 99, 235, 0.12);
}

.quote-detail-row td {
  background: var(--surface);
  border-top: 1px solid var(--surface-border);
  padding: 16px 24px;
}

.quote-detail {
  display: flex;
  flex-wrap: wrap;
  gap: 16px 32px;
}

.quote-detail__item {
  flex: 1 1 220px;
}

.quote-detail__item h4 {
  margin: 0 0 4px;
  font-size: 0.85rem;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--fg-muted);
}

.quote-detail__item p {
  margin: 0;
  font-weight: 600;
}

.quote-detail__notes {
  margin-top: 12px;
  font-size: 0.95rem;
  color: var(--fg-muted);
}

.sidebar {
  width: 240px;
  background: var(--surface);
  border-right: 1px solid var(--surface-border);
  padding: 24px 16px;
  display: flex;
  flex-direction: column;
  gap: 24px;
  transition: width 0.2s ease;
  position: fixed;
  top: 0;
  left: 0;
  bottom: 0;
  height: 100vh;
  overflow-y: auto;
  overflow-x: visible;
  z-index: 900;
}

.sidebar__brand {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  position: relative;
}

.sidebar__brand-link {
  display: flex;
  align-items: center;
  gap: 12px;
  text-decoration: none;
  color: var(--fg);
  font-weight: 700;
  font-size: 1.2rem;
  width: 100%;
}

.sidebar__brand-link--has-logo .sidebar__brand-text {
  display: none;
}

.sidebar__logo {
  width: 100%;
  height: auto;
  max-height: 80px;
  border-radius: 0;
  object-fit: contain;
  display: block;
}

.sidebar__brand-text {
  white-space: nowrap;
}

.sidebar__toggle {
  border: none;
  background: #fff;
  border-radius: 12px;
  width: 36px;
  height: 36px;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  color: var(--fg-muted);
  transition: background 0.2s ease, color 0.2s ease, transform 0.2s ease;
  position: fixed;
  left: 235px;
  top: 72px;
  transform: translateX(50%);
  z-index: 1200;
  box-shadow: 0 8px 24px rgba(15, 23, 42, 0.25);
}

.sidebar__toggle:hover {
  background: rgba(37, 99, 235, 0.12);
  color: var(--accent);
}

.nav-links {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.nav-links[data-nav-ready="false"] .nav-link {
  visibility: hidden;
}

.nav-link {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 12px;
  border-radius: 12px;
  text-decoration: none;
  color: var(--fg-muted);
  font-weight: 600;
  transition: background 0.15s ease, color 0.15s ease;
}

.nav-link.active,
.nav-link:hover {
  background: rgba(37, 99, 235, 0.12);
  color: var(--accent);
}

.nav-icon {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 24px;
  height: 24px;
  color: inherit;
}

.nav-icon svg {
  width: 20px;
  height: 20px;
}

.nav-label {
  white-space: nowrap;
}

.sidebar__footer {
  margin-top: auto;
  display: flex;
  flex-direction: column;
  gap: 16px;
  padding-bottom: 40px;
}

.sidebar__user-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  width: 100%;
}

.sidebar__user-row .button {
  flex-shrink: 0;
}

.sidebar__user {
  display: flex;
  align-items: center;
  gap: 12px;
  font-weight: 600;
}

.sidebar__user-avatar {
  width: 40px;
  height: 40px;
  border-radius: 14px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(148, 163, 184, 0.2);
}

.sidebar__footer .button {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
}

.sidebar__footer .button .nav-icon svg {
  width: 20px;
  height: 20px;
}

.button--icon {
  padding: 8px;
  width: 48px;
  height: 48px;
  border-radius: 16px;
  cursor: pointer;
}

.button--icon .nav-icon {
  width: 100%;
  height: 100%;
}

.button--icon .nav-icon svg {
  width: 24px;
  height: 24px;
}

.branding-wrapper {
  display: flex;
  align-items: center;
  gap: 24px;
  flex-wrap: wrap;
}

.branding-preview {
  width: 160px;
  height: 160px;
  border: 1px dashed rgba(148, 163, 184, 0.5);
  border-radius: 18px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(248, 250, 252, 0.7);
  overflow: hidden;
}

.branding-preview img {
  max-width: 100%;
  max-height: 100%;
  object-fit: contain;
}

#brandingLogoPlaceholder {
  color: var(--fg-muted);
  text-align: center;
  padding: 0 12px;
  font-size: 0.9rem;
}

.branding-actions {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.branding-actions .button {
  width: max-content;
}

.sidebar--collapsed {
  width: 76px;
}

.sidebar--collapsed .sidebar__brand-text,
.sidebar--collapsed .nav-label,
.sidebar--collapsed #sessionUser,
.sidebar--collapsed .button-label {
  display: none;
}

.sidebar--collapsed .sidebar__brand-link {
  justify-content: center;
}

body.sidebar-collapsed .sidebar__toggle {
  left: 76px;
  transform: translateX(50%) rotate(180deg);
}

.sidebar--collapsed .sidebar__footer .button {
  justify-content: center;
  width: 48px;
  height: 48px;
  padding: 0;
}

body.sidebar-collapsed .sidebar {
  width: 76px;
}

.content[data-permission-ready="false"] > :not(.topbar) {
  display: none !important;
}

.content--access-denied > :not(.topbar):not(.access-denied-message) {
  display: none !important;
}

.access-denied-message {
  margin: 48px auto;
  max-width: 520px;
  background: #fff1f2;
  border: 1px solid #fecdd3;
  color: #9f1239;
  padding: 32px;
  border-radius: 16px;
  box-shadow: 0 15px 30px rgba(15, 23, 42, 0.08);
  text-align: center;
}

.access-denied-message h2 {
  margin-top: 0;
  margin-bottom: 12px;
  font-size: 1.6rem;
}

.access-denied-message p {
  margin: 0;
  font-size: 1rem;
  color: #881337;
}

.nav-links a {
  text-decoration: none;
  color: var(--fg-muted);
  padding: 10px 12px;
  border-radius: 8px;
  font-weight: 500;
  transition: background 0.15s ease, color 0.15s ease;
}

.nav-links a.active,
.nav-links a:hover {
  background: rgba(37, 99, 235, 0.1);
  color: var(--accent);
}

.content {
  flex: 1;
  padding: 32px 60px;
  overflow-y: auto;
  background: linear-gradient(180deg, rgba(248,250,252,1) 0%, rgba(241,245,249,1) 60%, rgba(248,250,252,1) 100%);
  margin-left: 240px;
  position: relative;
  z-index: 0;
}

body.sidebar-collapsed .content {
  margin-left: 76px;
}

.topbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 32px;
}

.topbar__title {
  font-size: 1.5rem;
  font-weight: 600;
}

.page-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 24px;
}

.page-header__actions {
  margin-left: auto;
  display: inline-flex;
  align-items: center;
  gap: 12px;
}

.card {
  background: var(--surface);
  border-radius: 16px;
  border: 1px solid rgba(148, 163, 184, 0.2);
  padding: 20px 24px;
  box-shadow: 0 10px 30px rgba(15, 23, 42, 0.05);
  margin-bottom: 24px;
}

.card h2 {
  margin-top: 0;
  font-size: 1.25rem;
}

.toolbar {
  display: flex;
  flex-wrap: wrap;
  gap: 12px;
  margin-bottom: 16px;
}

.toolbar label {
  display: flex;
  flex-direction: column;
  gap: 6px;
  font-weight: 600;
  color: var(--fg-muted);
}

.toolbar input,
.toolbar select,
.toolbar button {
  font: inherit;
  padding: 10px 14px;
  border-radius: 10px;
  border: 1px solid rgba(148, 163, 184, 0.4);
  background: #fff;
  transition: border 0.15s ease, box-shadow 0.15s ease;
}

.toolbar button {
  background: var(--accent);
  color: var(--accent-fg);
  border-color: transparent;
  cursor: pointer;
}

.button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 10px 18px;
  border-radius: 10px;
  font-weight: 600;
  text-decoration: none;
  border: 1px solid rgba(37, 99, 235, 0.6);
  transition: background 0.15s ease, color 0.15s ease, box-shadow 0.15s ease;
}

.button.primary {
  background: var(--accent);
  color: var(--accent-fg);
  border-color: transparent;
}

.button.primary:hover {
  box-shadow: 0 8px 18px rgba(37, 99, 235, 0.25);
}

.button.secondary {
  background: #fff;
  color: var(--accent);
  border-color: rgba(37, 99, 235, 0.45);
}

.button.success {
  background: var(--success);
  color: #fff;
  border-color: transparent;
}

.button.danger {
  background: #dc2626;
  color: #fff;
  border-color: transparent;
}

.button.ghost {
  background: transparent;
  color: var(--accent);
  border-color: transparent;
}

.button.small {
  padding: 8px 14px;
  font-size: 0.875rem;
}

.quote-builder {
  display: flex;
  flex-direction: column;
  gap: 24px;
}

.quote-builder__header {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 16px;
}

.quote-builder__header-info h1 {
  margin: 0;
  font-size: 1.75rem;
}

.quote-builder__ref {
  display: flex;
  align-items: center;
  gap: 12px;
}

.quote-builder__ref-code {
  font-weight: 600;
  font-size: 1.1rem;
}

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

.quote-builder__body {
  display: grid;
  grid-template-columns: minmax(0, 2fr) minmax(260px, 1fr);
  gap: 24px;
  align-items: start;
}

.quote-builder__main {
  display: flex;
  flex-direction: column;
  gap: 24px;
}

.quote-builder__sidebar {
  display: flex;
  flex-direction: column;
  gap: 24px;
}

.qb-card {
  background: var(--surface);
  border-radius: 16px;
  border: 1px solid rgba(148, 163, 184, 0.25);
  padding: 20px 24px;
  box-shadow: 0 12px 30px rgba(15, 23, 42, 0.08);
}

.qb-card--sticky {
  position: sticky;
  top: 24px;
}

.qb-card__header {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 12px;
  margin-bottom: 20px;
}

.qb-card__header h2 {
  margin: 0;
  font-size: 1.2rem;
}

.qb-card__subtitle {
  margin: 4px 0 0;
  color: var(--fg-muted);
  font-size: 0.9rem;
}

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

.qb-grid {
  display: grid;
  gap: 16px;
}

.qb-grid--two {
  grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
}

.qb-field-display {
  padding: 12px 14px;
  border-radius: 10px;
  background: rgba(148, 163, 184, 0.12);
  min-height: 44px;
  white-space: pre-wrap;
}

.qb-tabs {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
  margin-bottom: 16px;
}

.qb-tab {
  border: 1px solid rgba(148, 163, 184, 0.35);
  padding: 8px 14px;
  border-radius: 10px;
  background: #fff;
  cursor: pointer;
  font-weight: 600;
}

.qb-tab[aria-selected="true"] {
  background: var(--accent);
  border-color: transparent;
  color: #fff;
  box-shadow: 0 6px 16px rgba(37, 99, 235, 0.26);
}

.qb-panels {
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.qb-panel {
  border: 1px solid rgba(148, 163, 184, 0.25);
  border-radius: 14px;
  padding: 16px;
  background: rgba(248, 250, 252, 0.8);
}

.qb-panel:not([data-active="true"]) {
  display: none;
}

.qb-panel__header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 12px;
}

.qb-option-form {
  display: flex;
  flex-direction: column;
  gap: 16px;
  margin-bottom: 16px;
}

.qb-option-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  justify-content: flex-end;
}

.qb-discount {
  margin-top: 12px;
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.qb-discount__label {
  font-weight: 600;
}

.qb-discount__controls {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  align-items: center;
}

.qb-option-discount-input {
  width: 160px;
  padding: 10px 12px;
  border-radius: 8px;
  border: 1px solid rgba(148, 163, 184, 0.4);
  font: inherit;
}

.qb-panel__title {
  font-size: 1rem;
  margin: 0;
}

.qb-accordion {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.qb-accordion details {
  border: 1px solid rgba(148, 163, 184, 0.2);
  border-radius: 12px;
  background: #fff;
  padding: 12px 16px;
}

.qb-accordion summary {
  list-style: none;
}

.qb-accordion summary::-webkit-details-marker {
  display: none;
}

.qb-accordion .qb-lines {
  margin-top: 12px;
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.qb-section-summary {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
  cursor: pointer;
  font-weight: 600;
}

.qb-section-summary__left {
  display: flex;
  align-items: center;
  gap: 12px;
}

.qb-section-icon {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 32px;
  height: 32px;
  border-radius: 10px;
  background: rgba(148, 163, 184, 0.16);
  color: var(--fg-muted);
}

.qb-section-icon svg {
  width: 20px;
  height: 20px;
}

.qb-section-title {
  display: inline-flex;
  align-items: baseline;
  gap: 6px;
}

.qb-section-count {
  font-size: 0.85rem;
  color: var(--fg-muted);
}

.qb-section-chevron {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 24px;
  height: 24px;
  color: var(--fg-muted);
}

.qb-lines__actions {
  display: flex;
  justify-content: flex-end;
}

.qb-lines__list {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.qb-line {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  padding: 10px 12px;
  border-radius: 10px;
  background: rgba(148, 163, 184, 0.1);
}

.qb-line__info {
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.qb-line__meta {
  font-size: 0.85rem;
  color: var(--fg-muted);
}

.qb-line__total {
  font-weight: 600;
}

.qb-line__actions {
  display: flex;
  gap: 6px;
  align-items: center;
}

.category-list {
  list-style: none;
  padding: 0;
  margin: 16px 0 0;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.category-list li {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 10px 14px;
  border-radius: 10px;
  background: rgba(148, 163, 184, 0.1);
}

.qb-summary {
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.qb-summary__option {
  border-bottom: 1px solid rgba(148, 163, 184, 0.2);
  padding-bottom: 12px;
}

.qb-summary__list {
  margin: 8px 0 0;
  padding: 0;
  list-style: none;
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.qb-summary__list li {
  display: flex;
  justify-content: space-between;
  font-size: 0.9rem;
}

.qb-summary__total {
  font-weight: 700;
  font-size: 1.05rem;
}

.qb-upload {
  display: flex;
  flex-direction: column;
  gap: 12px;
  align-items: flex-start;
}

.qb-muted {
  color: var(--fg-muted);
  font-size: 0.9rem;
}

.qb-overlay {
  position: fixed;
  inset: 0;
  background: rgba(15, 23, 42, 0.55);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
  padding: 24px;
}

.qb-overlay[hidden] {
  display: none !important;
}

.qb-modal {
  background: #fff;
  border-radius: 18px;
  max-width: 900px;
  width: 100%;
  box-shadow: 0 25px 60px rgba(15, 23, 42, 0.25);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.qb-modal--wide {
  max-width: 720px;
}

.qb-modal__header,
.qb-modal__footer {
  padding: 20px 24px;
  background: rgba(248, 250, 252, 0.9);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

.qb-modal__body {
  padding: 24px;
  max-height: 70vh;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.qb-modal__close {
  border: none;
  background: transparent;
  font-size: 1.5rem;
  cursor: pointer;
  line-height: 1;
}

.permission-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
  gap: 12px;
  padding: 12px 0;
}

.permission-grid label {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px 14px;
  border: 1px solid rgba(148, 163, 184, 0.4);
  border-radius: 12px;
  background: rgba(248, 250, 252, 0.8);
}

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

.qb-modal__controls input {
  flex: 1;
  min-width: 220px;
  padding: 10px 14px;
  border-radius: 10px;
  border: 1px solid rgba(148, 163, 184, 0.4);
}

.qb-modal__table {
  border: 1px solid rgba(148, 163, 184, 0.25);
  border-radius: 12px;
  overflow: hidden;
}

.qb-picker-selection {
  font-size: 0.95rem;
}

.qb-modal__footer-actions {
  display: flex;
  gap: 8px;
}

.qb-form-grid {
  display: grid;
  gap: 12px;
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
}

.qb-form-grid label {
  display: flex;
  flex-direction: column;
  gap: 6px;
  font-weight: 600;
  color: var(--fg-muted);
}

.qb-form-grid input,
.qb-form-grid select,
.qb-form-grid textarea {
  font: inherit;
  padding: 10px 12px;
  border-radius: 8px;
  border: 1px solid rgba(148, 163, 184, 0.4);
}

.qb-form-grid input[readonly] {
  background: rgba(148, 163, 184, 0.08);
  cursor: default;
}

.qb-line-empty {
  font-size: 0.9rem;
  color: var(--fg-muted);
  padding: 8px 0;
}

@media (max-width: 1200px) {
  .quote-builder__body {
    grid-template-columns: 1fr;
  }
  .qb-card--sticky {
    position: static;
  }
}

@media (max-width: 768px) {
  .quote-builder__header {
    flex-direction: column;
    align-items: flex-start;
  }
  .quote-builder__header-actions {
    width: 100%;
  }
  .qb-modal {
    max-width: 100%;
  }
}

table.data-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.95rem;
}

table.data-table thead {
  background: rgba(148, 163, 184, 0.15);
}

table.data-table th,
table.data-table td {
  padding: 12px 14px;
  text-align: left;
  border-bottom: 1px solid rgba(148, 163, 184, 0.25);
}

table.data-table tbody tr:hover {
  background: rgba(37, 99, 235, 0.08);
}

.status-pill {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 4px 10px;
  border-radius: 999px;
  font-weight: 600;
  font-size: 0.8rem;
}

.status-pill[data-status="draft"] { background: rgba(148, 163, 184, 0.18); color: #475569; }
.status-pill[data-status="sent"] { background: rgba(37, 99, 235, 0.18); color: #1d4ed8; }
.status-pill[data-status="accepted"] { background: rgba(22, 163, 74, 0.18); color: #15803d; }
.status-pill[data-status="declined"] { background: rgba(220, 38, 38, 0.18); color: #b91c1c; }
.status-pill[data-status="expired"] { background: rgba(202, 138, 4, 0.18); color: #b45309; }

.visually-hidden {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

@media (max-width: 900px) {
  body {
    flex-direction: column;
  }
  .sidebar {
    width: 100%;
    flex-direction: column;
    align-items: stretch;
    gap: 16px;
    position: static;
    height: auto;
    overflow: visible;
    z-index: 1;
  }
  .sidebar__toggle {
    position: static;
    transform: none;
    box-shadow: none;
    left: auto;
    top: auto;
    margin-left: auto;
  }
  body.sidebar-collapsed .sidebar__toggle {
    transform: rotate(180deg);
    left: auto;
  }
  .nav-links {
    flex-direction: row;
    flex-wrap: wrap;
  }
  .sidebar__footer {
    flex-direction: row;
    align-items: center;
    justify-content: space-between;
  }
  .sidebar__footer .button {
    width: auto;
    padding: 10px 16px;
  }
  .content {
    padding: 24px;
    margin-left: 0;
  }
  body.sidebar-collapsed .content {
    margin-left: 0;
  }
}
