:root {
  --bg: #eef2f5;
  --bg-accent: #e3ebf2;
  --surface: #ffffff;
  --ink: #152033;
  --muted: #5b6b7c;
  --line: #d5dee7;
  --brand: #0f2744;
  --brand-soft: #1c3d5e;
  --accent: #2f6f6a;
  --accent-hover: #255955;
  --danger: #9b2c2c;
  --danger-bg: #fdecec;
  --shadow: 0 12px 40px rgba(15, 39, 68, 0.08);
  --radius: 14px;
  --font: "Cairo", "Segoe UI", Tahoma, sans-serif;
}

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

html,
body {
  margin: 0;
  min-height: 100%;
}

body {
  font-family: var(--font);
  color: var(--ink);
  background:
    radial-gradient(1200px 500px at 100% -10%, rgba(47, 111, 106, 0.12), transparent 55%),
    radial-gradient(900px 420px at 0% 0%, rgba(15, 39, 68, 0.1), transparent 50%),
    linear-gradient(180deg, var(--bg) 0%, var(--bg-accent) 100%);
  line-height: 1.6;
}

body.preview-open {
  overflow: hidden;
}

.app-shell {
  width: min(980px, calc(100% - 2rem));
  margin: 0 auto;
  padding: 2rem 0 3rem;
}

.app-header {
  margin-bottom: 1.5rem;
}

.app-header h1 {
  margin: 0;
  font-size: clamp(1.8rem, 3vw, 2.35rem);
  font-weight: 800;
  letter-spacing: -0.02em;
  color: var(--brand);
}

.app-header p {
  margin: 0.35rem 0 0;
  color: var(--muted);
  max-width: 40rem;
}

.panel {
  background: var(--surface);
  border: 1px solid rgba(213, 222, 231, 0.9);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 1.25rem 1.35rem 1.4rem;
  margin-bottom: 1rem;
}

.panel h2 {
  margin: 0 0 1rem;
  font-size: 1.05rem;
  font-weight: 700;
  color: var(--brand);
}

.grid-3 {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 0.9rem;
}

label.field {
  display: flex;
  flex-direction: column;
  gap: 0.35rem;
  font-size: 0.92rem;
  font-weight: 600;
  color: var(--ink);
}

.input {
  width: 100%;
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 0.65rem 0.8rem;
  font: inherit;
  color: var(--ink);
  background: #fbfcfd;
  transition: border-color 0.15s ease, box-shadow 0.15s ease;
}

.input:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(47, 111, 106, 0.15);
  background: #fff;
}

.table-wrap {
  overflow-x: auto;
  border: 1px solid var(--line);
  border-radius: 12px;
}

.items-table {
  width: 100%;
  border-collapse: collapse;
  min-width: 640px;
}

.items-table th,
.items-table td {
  padding: 0.65rem 0.7rem;
  border-bottom: 1px solid var(--line);
  vertical-align: middle;
}

.items-table th {
  text-align: start;
  font-size: 0.85rem;
  color: var(--muted);
  background: #f5f8fb;
  font-weight: 700;
}

.items-table tr:last-child td {
  border-bottom: none;
}

.items-table .num {
  font-variant-numeric: tabular-nums;
  font-weight: 700;
  color: var(--brand);
  white-space: nowrap;
}

.col-actions {
  width: 1%;
  white-space: nowrap;
}

.items-toolbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 1rem;
  margin-top: 0.9rem;
  flex-wrap: wrap;
}

.total-box {
  display: flex;
  align-items: baseline;
  gap: 0.6rem;
  font-weight: 700;
}

.total-box span:last-child {
  font-size: 1.25rem;
  color: var(--accent);
  font-variant-numeric: tabular-nums;
}

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

.btn {
  appearance: none;
  border: none;
  border-radius: 10px;
  padding: 0.7rem 1.1rem;
  font: inherit;
  font-weight: 700;
  cursor: pointer;
  transition: background 0.15s ease, transform 0.1s ease, opacity 0.15s ease;
}

.btn:active {
  transform: translateY(1px);
}

.btn:disabled {
  opacity: 0.65;
  cursor: wait;
}

.btn--primary {
  background: var(--brand);
  color: #fff;
}

.btn--primary:hover {
  background: var(--brand-soft);
}

.btn--accent {
  background: var(--accent);
  color: #fff;
}

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

.btn--secondary {
  background: #e8eef4;
  color: var(--brand);
}

.btn--secondary:hover {
  background: #dbe5ef;
}

.btn--whatsapp {
  background: #1f8f4e;
  color: #fff;
}

.btn--whatsapp:hover {
  background: #187a42;
}

.btn--ghost {
  background: transparent;
  color: var(--danger);
  border: 1px solid rgba(155, 44, 44, 0.25);
}

.btn--ghost:hover {
  background: var(--danger-bg);
}

.btn--small {
  padding: 0.4rem 0.7rem;
  font-size: 0.85rem;
}

.form-error {
  margin: 0 0 0.9rem;
  padding: 0.75rem 0.9rem;
  border-radius: 10px;
  background: var(--danger-bg);
  color: var(--danger);
  border: 1px solid rgba(155, 44, 44, 0.2);
  font-weight: 600;
}

.notice {
  margin: 0.9rem 0 0;
  padding: 0.75rem 0.9rem;
  border-radius: 10px;
  background: #f3f7f7;
  color: var(--muted);
  border: 1px dashed rgba(47, 111, 106, 0.35);
  font-size: 0.9rem;
}

.panel-help {
  margin: -0.35rem 0 0.9rem;
  color: var(--muted);
  font-size: 0.92rem;
}

.template-status {
  margin: 0 0 0.85rem;
  font-weight: 700;
  color: var(--brand);
}

.template-dims {
  width: min(100%, calc(210mm + 2rem));
  margin: 0 auto 0.75rem;
  color: #fff;
  font-weight: 600;
}

.template-preview-image {
  display: block;
  max-width: min(100%, 794px);
  height: auto;
  background: #fff;
  box-shadow: 0 18px 50px rgba(0, 0, 0, 0.28);
}

/* Preview modal */
.preview-modal {
  position: fixed;
  inset: 0;
  z-index: 50;
  background: rgba(10, 18, 28, 0.55);
  display: flex;
  flex-direction: column;
  padding: 1rem;
}

.preview-modal[hidden] {
  display: none !important;
}

.preview-bar {
  width: min(100%, calc(210mm + 2rem));
  margin: 0 auto 0.75rem;
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 0.75rem;
  flex-wrap: wrap;
  color: #fff;
}

.preview-bar h2 {
  margin: 0;
  font-size: 1.05rem;
}

.preview-bar__actions {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
}

.preview-scroll {
  flex: 1;
  overflow: auto;
  display: flex;
  justify-content: center;
  align-items: flex-start;
  padding-bottom: 1rem;
}

#invoice-stage {
  flex: 0 0 auto;
}

/* Invoice page overlay */
.invoice-page {
  position: relative;
  background: #fff;
  overflow: hidden;
  box-shadow: 0 18px 50px rgba(0, 0, 0, 0.28);
}

.invoice-page__bg {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  object-fit: fill;
  pointer-events: none;
  user-select: none;
}

.invoice-mask {
  position: absolute;
  z-index: 2;
  pointer-events: none;
}

.invoice-field {
  position: absolute;
  z-index: 5;
  white-space: nowrap;
  overflow: visible;
  color: #000;
  -webkit-font-smoothing: antialiased;
}

#print-root {
  position: absolute;
  left: -99999px;
  top: 0;
  width: 0;
  height: 0;
  overflow: hidden;
}

@media (max-width: 820px) {
  .grid-3 {
    grid-template-columns: 1fr;
  }

  .app-shell {
    width: min(100% - 1.25rem, 980px);
    padding-top: 1.25rem;
  }
}
