:root {
  --bg: #f5f4f0;
  --bg2: #eeeee9;
  --surface: #ffffff;
  --card: #ffffff;
  --card-hover: #fafaf9;
  --border: #e4e4e7;
  --border-hover: #d4d4d8;
  --accent: #e54e09;
  --accent-soft: rgba(229, 78, 9, 0.08);
  --green: #16a34a;
  --green-soft: rgba(22, 163, 74, 0.08);
  --blue: #2563eb;
  --blue-soft: rgba(37, 99, 235, 0.08);
  --yellow: #ca8a04;
  --yellow-soft: rgba(202, 138, 4, 0.08);
  --ink: #18181b;
  --sub: #71717a;
  --sub2: #a1a1aa;
  --mono: "IBM Plex Mono", monospace;
  --disp: "Space Grotesk", sans-serif;
  --body: "Inter", sans-serif;
  --radius: 12px;
  --radius-sm: 8px;
  --shadow-sm: 0 1px 3px rgba(0, 0, 0, 0.06);
  --shadow-md: 0 4px 16px rgba(0, 0, 0, 0.08);
  --sidebar-w: 240px;
  --panel-w: 380px;
}

[data-theme="dark"] {
  --bg: #0f0f0f;
  --bg2: #1a1a1a;
  --surface: #1e1e1e;
  --card: #1e1e1e;
  --card-hover: #252525;
  --border: #2e2e2e;
  --border-hover: #3e3e3e;
  --accent: #e54e09;
  --accent-soft: rgba(229, 78, 9, 0.12);
  --green: #22c55e;
  --green-soft: rgba(34, 197, 94, 0.12);
  --blue: #3b82f6;
  --blue-soft: rgba(59, 130, 246, 0.12);
  --yellow: #eab308;
  --yellow-soft: rgba(234, 179, 8, 0.12);
  --ink: #f0f0f0;
  --sub: #a1a1aa;
  --sub2: #71717a;
  --shadow-sm: 0 1px 3px rgba(0, 0, 0, 0.2);
  --shadow-md: 0 4px 16px rgba(0, 0, 0, 0.3);
}

*,
*::before,
*::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

body {
  background: var(--bg);
  color: var(--ink);
  font-family: var(--body);
  -webkit-font-smoothing: antialiased;
  min-height: 100dvh;
  overflow: hidden;
}

.hidden {
  display: none !important;
}
.page-section.hidden {
  display: none !important;
}

/* ---- AUTH PAGE ---- */
.auth-page {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 100dvh;
  padding: 24px;
}

.auth-card {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 40px;
  width: 100%;
  max-width: 400px;
  box-shadow: var(--shadow-md);
}

.auth-brand {
  font-family: var(--disp);
  font-weight: 700;
  font-size: 20px;
  text-align: center;
  margin-bottom: 4px;
}

.auth-brand .accent {
  color: var(--accent);
}
.auth-sub {
  text-align: center;
  font-size: 13px;
  color: var(--sub);
  margin-bottom: 28px;
}

.auth-error {
  background: var(--accent-soft);
  border: 1px solid rgba(229, 78, 9, 0.2);
  color: var(--accent);
  padding: 10px 14px;
  border-radius: var(--radius-sm);
  font-size: 12px;
  margin-bottom: 16px;
  display: none;
}

/* ---- BUTTONS & FIELDS ---- */
.btn-submit {
  width: 100%;
  padding: 11px;
  font-family: var(--body);
  font-size: 13px;
  font-weight: 600;
  border: none;
  border-radius: var(--radius-sm);
  cursor: pointer;
  transition: all 0.2s;
  color: #fff;
}
.btn-submit.green {
  background: var(--green);
}
.btn-submit.red {
  background: var(--accent);
}
.btn-submit.blue {
  background: var(--blue);
}
.btn-submit:hover {
  opacity: 0.88;
}
.btn-submit:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.btn-secondary {
  padding: 8px 16px;
  font-family: var(--body);
  font-size: 12px;
  font-weight: 600;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--surface);
  color: var(--ink);
  cursor: pointer;
  transition: all 0.2s;
}
.btn-secondary:hover {
  border-color: var(--accent);
  color: var(--accent);
}

.field {
  margin-bottom: 12px;
}
.field label {
  display: block;
  font-size: 11px;
  font-weight: 600;
  color: var(--sub);
  margin-bottom: 4px;
  letter-spacing: 0.04em;
}
.field input,
.field textarea,
.field select {
  width: 100%;
  background: var(--surface);
  border: 1px solid var(--border);
  color: var(--ink);
  padding: 9px 12px;
  font-family: var(--body);
  font-size: 13px;
  border-radius: var(--radius-sm);
  transition: border-color 0.2s;
}
.field input:focus,
.field textarea:focus,
.field select:focus {
  outline: none;
  border-color: var(--accent);
}
.field textarea {
  resize: vertical;
  min-height: 48px;
}

/* ---- LAYOUT ---- */
.app-layout {
  display: flex;
  height: 100dvh;
}

.sidebar {
  width: var(--sidebar-w);
  background: var(--card);
  border-right: 1px solid var(--border);
  padding: 20px 0;
  display: flex;
  flex-direction: column;
  flex-shrink: 0;
  z-index: 100;
}

.sidebar-brand {
  font-family: var(--disp);
  font-weight: 700;
  font-size: 14px;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  padding: 0 20px;
  margin-bottom: 4px;
}
.sidebar-brand .accent {
  color: var(--accent);
}
.sidebar-sub {
  font-size: 11px;
  color: var(--sub2);
  padding: 0 20px;
  margin-bottom: 24px;
}

.sidebar-nav {
  flex: 1;
  overflow-y: auto;
}
.nav-section-label {
  font-size: 10px;
  font-weight: 600;
  letter-spacing: 0.1em;
  text-transform: uppercase;
  color: var(--sub2);
  padding: 16px 20px 8px;
}

.nav-item {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 20px;
  font-size: 13px;
  font-weight: 500;
  color: var(--sub);
  cursor: pointer;
  transition: all 0.15s;
  border: none;
  background: none;
  width: 100%;
  text-align: left;
  font-family: var(--body);
}
.nav-item:hover {
  background: var(--bg);
  color: var(--ink);
}
.nav-item.active {
  background: var(--accent-soft);
  color: var(--accent);
  font-weight: 600;
}

.sidebar-footer {
  border-top: 1px solid var(--border);
  padding: 16px 20px;
}
.sidebar-user {
  font-size: 12px;
  color: var(--sub);
  margin-bottom: 8px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

/* ---- MAIN WORKSPACE ---- */
.main-content {
  flex: 1;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.top-bar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 12px 24px;
  background: var(--card);
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}

.top-bar-title {
  font-family: var(--disp);
  font-size: 16px;
  font-weight: 600;
}

.status-dot {
  display: inline-block;
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: #ccc;
  margin-right: 6px;
}
.status-dot.live {
  background: var(--green);
}

/* ---- SPLIT VIEW ---- */
.split-view {
  display: flex;
  flex: 1;
  overflow: hidden;
}

.control-panel {
  width: var(--panel-w);
  background: var(--surface);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  flex-shrink: 0;
}

.panel-content {
  flex: 1;
  overflow-y: auto;
  padding: 20px;
}

.panel-footer {
  padding: 16px 20px;
  border-top: 1px solid var(--border);
  background: var(--card);
}

.panel-section-title {
  font-family: var(--disp);
  font-size: 12px;
  letter-spacing: 0.1em;
  text-transform: uppercase;
  color: var(--sub);
  margin: 24px 0 12px;
  padding-bottom: 4px;
  border-bottom: 1px solid var(--border);
}
.panel-section-title:first-child {
  margin-top: 0;
}

.preview-wrapper {
  flex: 1;
  background: #e5e7eb; /* Fixed grey background for contrast with white paper */
  overflow-y: auto;
  padding: 40px 20px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 20px;
}

[data-theme="dark"] .preview-wrapper {
  background: #111827; /* Darker grey for dark mode */
}

/* ---- A4 PAPER (ALWAYS LIGHT MODE) ---- */
.a4-page {
  background: #ffffff !important; /* Force white paper */
  color: #000000 !important; /* Force black ink */
  width: 210mm;
  min-height: 297mm;
  box-shadow: 0 10px 25px rgba(0, 0, 0, 0.15);
  position: relative;
  border-radius: 2px;
  flex-shrink: 0;
  /* Print Specific Paddings applied within */
}

/* Base print styling container applied INSIDE A4 */
.print-content {
  padding: 40px 48px;
}

/* ---- DOCUMENT COMPONENTS (SHARED PRINT STYLES) ---- */
.c-head {
  text-align: center;
  margin-bottom: 36px;
}
.c-brand {
  font-family: var(--disp);
  font-weight: 700;
  font-size: 20px;
  letter-spacing: 0.03em;
  text-transform: uppercase;
  margin-bottom: 6px;
}
.c-doctitle {
  font-size: 11px;
  letter-spacing: 0.1em;
  color: #555 !important;
  text-transform: uppercase;
}

.c-meta-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
  margin-bottom: 24px;
  padding-bottom: 18px;
  border-bottom: 1px solid #ccc !important;
}

.c-meta-item {
  display: flex;
  font-size: 13px;
}
.c-meta-item .label {
  font-weight: 600;
  width: 130px;
  color: #555 !important;
  flex-shrink: 0;
}
.c-meta-item .val {
  font-family: var(--mono);
  font-weight: 500;
}

.clause h3 {
  font-family: var(--disp);
  font-size: 13px;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  margin-top: 22px;
  margin-bottom: 8px;
  border-bottom: 1px solid #eee !important;
  padding-bottom: 4px;
}

.clause p {
  font-size: 12.5px;
  line-height: 1.6;
  margin: 0 0 10px 0;
  text-align: justify;
}

.signatures {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 40px;
  margin-top: 50px;
}
.sig-line {
  border-bottom: 1px solid #000 !important;
  height: 40px;
  margin-bottom: 5px;
}
.sig-label {
  font-size: 10px;
  color: #555 !important;
  font-weight: 600;
  text-transform: uppercase;
}

/* ---- INVOICE COMPONENTS ---- */
.i-head {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  margin-bottom: 24px;
}
.i-brand {
  font-family: var(--disp);
  font-weight: 600;
  font-size: 14px;
  letter-spacing: 0.03em;
  color: #555 !important;
  text-transform: uppercase;
  margin-bottom: 5px;
}
.i-doctitle {
  font-family: var(--disp);
  font-size: 26px;
  font-weight: 700;
  display: flex;
  align-items: center;
  gap: 10px;
}
.i-status {
  font-weight: 600;
  font-size: 11px;
  letter-spacing: 0.1em;
  text-transform: uppercase;
  color: var(--accent) !important;
  border: 1px solid var(--accent) !important;
  padding: 3px 9px;
  display: inline-block;
}

.i-meta {
  display: grid;
  grid-template-columns: 1fr 1fr 1fr;
  gap: 14px 20px;
  padding: 14px 0;
  border-top: 1px solid #ccc !important;
  border-bottom: 1px solid #ccc !important;
  margin-bottom: 24px;
}
.i-meta .k {
  font-size: 10px;
  letter-spacing: 0.1em;
  text-transform: uppercase;
  color: #555 !important;
  margin-bottom: 3px;
}
.i-meta .v {
  font-family: var(--mono);
  font-size: 13px;
}
.i-meta .v.strong {
  font-family: var(--disp);
  font-weight: 600;
  font-size: 14px;
}

.r-table {
  width: 100%;
  border-collapse: collapse;
  margin-bottom: 18px;
}
.r-table th {
  text-align: left;
  font-size: 10px;
  letter-spacing: 0.1em;
  text-transform: uppercase;
  color: #555 !important;
  padding: 0 8px 10px;
  border-bottom: 1px solid #000 !important;
}
.r-table th.num {
  text-align: right;
}
.r-table td {
  padding: 14px 8px;
  border-bottom: 1px solid #eee !important;
  font-size: 13px;
  vertical-align: top;
}
.r-table tr:last-child td {
  border-bottom: 1px solid #000 !important;
}
.r-table td.num {
  text-align: right;
  font-family: var(--mono);
  white-space: nowrap;
}

.totalbox {
  width: 280px;
  margin-left: auto;
}
.grand {
  display: flex;
  justify-content: space-between;
  align-items: center;
  background: #000 !important;
  color: #fff !important;
  padding: 14px 16px;
}
.grand .l {
  font-size: 11px;
  letter-spacing: 0.1em;
  text-transform: uppercase;
}
.grand .r {
  font-family: var(--mono);
  font-size: 18px;
  font-weight: 600;
}

.paybox {
  border: 1px solid #ccc !important;
  border-top: none;
  padding: 10px 16px 12px;
}
.paybox .k {
  font-size: 10px;
  letter-spacing: 0.1em;
  text-transform: uppercase;
  color: #555 !important;
  margin-bottom: 4px;
}
.paybox .v {
  font-family: var(--mono);
  font-size: 14px;
  font-weight: 600;
}

/* ---- TOAST & MODAL ---- */
.toast {
  position: fixed;
  bottom: 24px;
  right: 24px;
  z-index: 2000;
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 12px 20px;
  font-size: 13px;
  color: var(--ink);
  box-shadow: var(--shadow-md);
  transform: translateY(100px);
  opacity: 0;
  transition: all 0.3s ease;
}
.toast.show {
  transform: translateY(0);
  opacity: 1;
}
.toast.success {
  border-left: 3px solid var(--green);
}
.toast.error {
  border-left: 3px solid var(--accent);
}

/* ---- MASTER DATA DB ---- */
.db-wrapper {
  padding: 24px;
  flex: 1;
  overflow-y: auto;
  background: var(--bg);
}

.db-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(400px, 1fr));
  gap: 24px;
}

.db-card {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 20px;
}

.db-title {
  font-family: var(--disp);
  font-size: 16px;
  font-weight: 600;
  margin-bottom: 16px;
  padding-bottom: 8px;
  border-bottom: 1px solid var(--border);
}

.db-table {
  width: 100%;
  border-collapse: collapse;
}
.db-table th {
  text-align: left;
  padding: 8px;
  font-size: 11px;
  color: var(--sub);
  text-transform: uppercase;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  z-index: 10;
}
.db-table td {
  padding: 10px 8px;
  font-size: 13px;
  border-bottom: 1px solid var(--border);
}
.db-table tr:last-child td {
  border-bottom: none;
}

.table-scroll-container {
  max-height: 300px;
  overflow-y: auto;
  border: 1px solid var(--border);
  border-radius: 4px;
}

/* Custom Searchable Select (Combobox) */
.combo-box {
  position: relative;
  width: 100%;
}

.combo-input {
  width: 100%;
  background: var(--surface);
  border: 1px solid var(--border);
  color: var(--ink);
  padding: 9px 12px;
  font-family: var(--body);
  font-size: 13px;
  border-radius: var(--radius-sm);
  transition: border-color 0.2s;
  cursor: text;
}

.combo-input:focus {
  outline: none;
  border-color: var(--accent);
}

.combo-list {
  display: none;
  position: absolute;
  top: 100%;
  left: 0;
  right: 0;
  max-height: 200px;
  overflow-y: auto;
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  box-shadow: var(--shadow-md);
  z-index: 1000;
  margin-top: 4px;
}

.combo-list.open {
  display: block;
}

.combo-item {
  padding: 10px 12px;
  font-size: 13px;
  cursor: pointer;
  border-bottom: 1px solid var(--border-hover);
}

.combo-item:last-child {
  border-bottom: none;
}

.combo-item:hover {
  background: var(--accent-soft);
  color: var(--accent);
}

.combo-item-sub {
  font-size: 11px;
  color: var(--sub);
  margin-top: 2px;
}

.btn-icon {
  background: transparent;
  border: 1px solid var(--border);
  color: var(--sub2);
  padding: 4px 8px;
  font-size: 11px;
  border-radius: 4px;
  cursor: pointer;
  transition: all 0.2s;
}
.btn-icon:hover {
  border-color: var(--accent);
  color: var(--accent);
}

/* ---- MOBILE RESPONSIVENESS ---- */
.mobile-tabs {
  display: none;
}

@media (max-width: 1024px) {
  .a4-page {
    transform: scale(0.9);
    transform-origin: top center;
  }
}

@media (max-width: 768px) {
  .sidebar {
    position: fixed;
    transform: translateX(-100%);
    transition: transform 0.3s ease;
    height: 100%;
  }
  .sidebar.open {
    transform: translateX(0);
  }

  .top-bar-title {
    display: none;
  }
  .split-view {
    flex-direction: column;
  }

  .mobile-tabs {
    display: flex;
    background: var(--card);
    border-bottom: 1px solid var(--border);
    flex-shrink: 0;
  }

  .mobile-tab {
    flex: 1;
    padding: 12px;
    text-align: center;
    font-size: 13px;
    font-weight: 600;
    color: var(--sub);
    background: none;
    border: none;
    border-bottom: 2px solid transparent;
  }
  .mobile-tab.active {
    color: var(--accent);
    border-bottom-color: var(--accent);
  }

  .control-panel {
    width: 100%;
    flex: 1;
    display: none;
    border-right: none;
  }
  .control-panel.active {
    display: flex;
  }

  .preview-wrapper {
    display: none;
    padding: 20px 10px;
  }
  .preview-wrapper.active {
    display: flex;
  }

  .a4-page {
    transform: scale(0.45); /* Scale down A4 to fit mobile screen */
    transform-origin: top center;
    margin-bottom: -150mm; /* Adjust for scaling collapse */
  }

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

/* ---- PRINT MEDIA QUERIES ---- */
@media print {
  @page {
    size: A4;
    margin: 0;
  }

  body,
  html {
    width: 100%;
    height: 100%;
    margin: 0;
    padding: 0;
    background: #fff !important;
  }

  /* Hide UI */
  .sidebar,
  .top-bar,
  .control-panel,
  .mobile-tabs {
    display: none !important;
  }

  /* Reset Preview Wrapper to fill page */
  .split-view {
    display: block !important;
  }
  .preview-wrapper {
    display: block !important;
    padding: 0 !important;
    background: #fff !important;
    overflow: visible !important;
  }

  .a4-page {
    box-shadow: none !important;
    margin: 0 !important;
    border-radius: 0 !important;
    width: 210mm !important;
    min-height: 297mm !important;
    transform: none !important; /* Remove scaling for true print size */
    page-break-after: always;
  }

  .a4-page:last-child {
    page-break-after: avoid;
  }

  * {
    -webkit-print-color-adjust: exact !important;
    print-color-adjust: exact !important;
  }
}
