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

:root {
  --bg:        #0f172a;
  --surface:   #1e293b;
  --surface2:  #273549;
  --border:    #334155;
  --accent:    #e94560;
  --accent2:   #f5a623;
  --green:     #4ecca3;
  --blue:      #38bdf8;
  --text:      #e2e8f0;
  --muted:     #94a3b8;
  --paper-bg:  #fffef7;
  --paper-txt: #1a1a1a;
  --radius:    8px;
  --sidebar-w: 380px;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
  background: var(--bg);
  color: var(--text);
  font-size: 14px;
  height: 100dvh;
  overflow: hidden;
}

/* ── Layout ── */
.app {
  display: grid;
  grid-template-columns: var(--sidebar-w) 1fr;
  height: 100dvh;
}

/* ── Sidebar ── */
.sidebar {
  background: var(--surface);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.sidebar-header {
  padding: 20px 20px 14px;
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}

.logo-row {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 4px;
}

.logo-text {
  font-size: 20px;
  font-weight: 700;
  font-family: 'SF Mono', 'Fira Code', 'Courier New', monospace;
  color: var(--text);
  letter-spacing: -0.5px;
}

.logo-text .accent { color: var(--accent); }

.subtitle {
  font-size: 12px;
  color: var(--muted);
}

#receipt-form {
  flex: 1;
  overflow-y: auto;
  padding: 0 0 80px;
}

#receipt-form::-webkit-scrollbar { width: 4px; }
#receipt-form::-webkit-scrollbar-thumb { background: var(--border); border-radius: 2px; }

/* ── Form sections ── */
.form-section {
  padding: 16px 20px;
  border-bottom: 1px solid var(--border);
}

.section-title {
  font-size: 11px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.8px;
  color: var(--muted);
  margin-bottom: 10px;
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.section-hint {
  font-size: 11px;
  color: var(--muted);
  margin-bottom: 8px;
  line-height: 1.4;
}
.section-hint strong { color: var(--green); }

.field { margin-bottom: 8px; }
.field:last-child { margin-bottom: 0; }

.field label {
  display: block;
  font-size: 12px;
  color: var(--muted);
  margin-bottom: 4px;
}

.field input,
.field select {
  width: 100%;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--text);
  padding: 7px 10px;
  font-size: 13px;
  outline: none;
  transition: border-color 0.15s;
}

.field input:focus,
.field select:focus { border-color: var(--accent); }

.field-row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
  margin-bottom: 8px;
}

.field-row .field { margin-bottom: 0; }

/* ── Input with alignment buttons ── */
.input-with-align {
  display: flex;
  gap: 6px;
  align-items: center;
}
.input-with-align input { flex: 1; }

.align-btns {
  display: flex;
  gap: 2px;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 2px;
}

.align-btn {
  background: none;
  border: none;
  color: var(--muted);
  cursor: pointer;
  padding: 4px 5px;
  border-radius: 4px;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: color 0.15s, background 0.15s;
}

.align-btn:hover { color: var(--text); }
.align-btn.active {
  color: var(--accent);
  background: rgba(233,69,96,0.15);
}

/* ── Item / Tax / Custom rows ── */
.item-row, .tax-row, .custom-line-row {
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 10px;
  margin-bottom: 8px;
  position: relative;
}

.item-row-grid {
  display: grid;
  grid-template-columns: 1fr 48px 80px 80px;
  gap: 6px;
  align-items: end;
}

.tax-row-grid {
  display: grid;
  grid-template-columns: 1fr 60px 70px auto;
  gap: 6px;
  align-items: end;
}

.custom-line-grid {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 6px;
  align-items: end;
}

.item-row .field,
.tax-row .field,
.custom-line-row .field { margin-bottom: 0; }

.tax-computed {
  font-size: 11px;
  color: var(--green);
  margin-top: 4px;
  font-family: 'SF Mono', 'Fira Code', monospace;
}

.btn-remove {
  background: none;
  border: none;
  color: var(--muted);
  cursor: pointer;
  font-size: 16px;
  padding: 4px;
  line-height: 1;
  border-radius: 4px;
  transition: color 0.15s, background 0.15s;
  display: flex;
  align-items: center;
  justify-content: center;
  height: 32px;
  width: 32px;
  flex-shrink: 0;
}

.btn-remove:hover { color: var(--accent); background: rgba(233,69,96,0.1); }

.btn-add {
  background: none;
  border: 1px solid var(--border);
  color: var(--muted);
  cursor: pointer;
  font-size: 11px;
  padding: 3px 8px;
  border-radius: 4px;
  transition: color 0.15s, border-color 0.15s;
}

.btn-add:hover { color: var(--green); border-color: var(--green); }

/* ── Checkboxes ── */
.checkboxes { display: flex; flex-direction: column; gap: 8px; }

.checkbox-label {
  display: flex;
  align-items: center;
  gap: 8px;
  cursor: pointer;
  font-size: 13px;
  color: var(--text);
}

.checkbox-label input[type="checkbox"] {
  accent-color: var(--accent);
  width: 14px;
  height: 14px;
  cursor: pointer;
}

/* ── Build button ── */
.btn-build {
  position: sticky;
  bottom: 0;
  width: calc(100% - 40px);
  margin: 12px 20px;
  background: linear-gradient(135deg, var(--accent), var(--accent2));
  border: none;
  border-radius: var(--radius);
  color: #fff;
  font-size: 14px;
  font-weight: 600;
  padding: 12px;
  cursor: pointer;
  transition: opacity 0.15s, transform 0.1s;
}

.btn-build:hover { opacity: 0.9; }
.btn-build:active { transform: scale(0.98); }
.btn-build:disabled { opacity: 0.5; cursor: not-allowed; }

/* ── Preview pane ── */
.preview-pane {
  display: flex;
  flex-direction: column;
  overflow: hidden;
  background: var(--bg);
}

.preview-toolbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 20px;
  border-bottom: 1px solid var(--border);
  background: var(--surface);
  flex-shrink: 0;
}

.preview-title {
  font-size: 13px;
  font-weight: 600;
  color: var(--text);
}

.toolbar-actions { display: flex; gap: 6px; }

.btn-icon {
  display: flex;
  align-items: center;
  gap: 5px;
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--muted);
  cursor: pointer;
  font-size: 11px;
  padding: 5px 9px;
  transition: color 0.15s, border-color 0.15s;
  white-space: nowrap;
}

.btn-icon:hover:not(:disabled) { color: var(--text); border-color: var(--text); }
.btn-icon:disabled { opacity: 0.4; cursor: not-allowed; }

/* ── Output tabs ── */
.output-tabs {
  display: flex;
  gap: 0;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  padding: 0 20px;
  flex-shrink: 0;
}

.tab-btn {
  background: none;
  border: none;
  border-bottom: 2px solid transparent;
  color: var(--muted);
  cursor: pointer;
  font-size: 12px;
  font-weight: 500;
  padding: 10px 16px;
  transition: color 0.15s, border-color 0.15s;
}

.tab-btn:hover { color: var(--text); }
.tab-btn.active {
  color: var(--accent);
  border-bottom-color: var(--accent);
}

.tab-content {
  display: none;
  flex: 1;
  overflow: hidden;
}
.tab-content.active { display: flex; flex-direction: column; }

/* ── Receipt preview ── */
.receipt-wrapper {
  flex: 1;
  overflow-y: auto;
  display: flex;
  justify-content: center;
  padding: 32px 24px;
  background: var(--bg);
}

.receipt-paper {
  background: var(--paper-bg);
  color: var(--paper-txt);
  width: 100%;
  max-width: 480px;
  min-height: 200px;
  border-radius: 4px 4px 0 0;
  box-shadow: 0 4px 24px rgba(0,0,0,0.4);
  padding: 20px;
  position: relative;
}

/* Tear effect at bottom */
.receipt-paper::after {
  content: '';
  display: block;
  position: absolute;
  bottom: -10px;
  left: 0;
  right: 0;
  height: 10px;
  background: repeating-linear-gradient(
    90deg,
    var(--bg) 0px, var(--bg) 8px,
    var(--paper-bg) 8px, var(--paper-bg) 12px
  );
  mask: url("data:image/svg+xml,%3Csvg viewBox='0 0 20 10' xmlns='http://www.w3.org/2000/svg'%3E%3Ccircle cx='10' cy='0' r='10' fill='black'/%3E%3C/svg%3E");
  mask-size: 20px 10px;
  -webkit-mask: url("data:image/svg+xml,%3Csvg viewBox='0 0 20 10' xmlns='http://www.w3.org/2000/svg'%3E%3Ccircle cx='10' cy='0' r='10' fill='black'/%3E%3C/svg%3E");
  -webkit-mask-size: 20px 10px;
  background-color: var(--paper-bg);
}

/* ── Rich receipt content ── */
.receipt-content {
  font-family: 'Courier New', Courier, monospace;
  font-size: 13px;
  line-height: 1.6;
  color: var(--paper-txt);
  overflow-x: hidden;
}

.r-line {
  white-space: pre-wrap;
  word-break: break-word;
  min-height: 1.6em;
}
.r-center { text-align: center; }
.r-right  { text-align: right; }
.r-left   { text-align: left; }
.r-bold   { font-weight: 700; }
.r-big    { font-size: 20px; font-weight: 700; }
.r-dbl-h  { font-size: 16px; }
.r-underline { text-decoration: underline; }

.r-divider {
  white-space: pre;
  color: var(--paper-txt);
  opacity: 0.5;
  letter-spacing: 1px;
}

.r-row {
  display: flex;
  justify-content: space-between;
  gap: 8px;
}
.r-row-left  { flex: 1; text-align: left; overflow: hidden; text-overflow: ellipsis; }
.r-row-right { text-align: right; white-space: nowrap; font-weight: 600; }
.r-row-total { font-weight: 700; font-size: 15px; }

.r-barcode {
  text-align: center;
  padding: 12px 0;
}
.r-barcode-bars {
  display: flex;
  justify-content: center;
  gap: 1px;
  margin-bottom: 4px;
}
.r-barcode-bar {
  width: 2px;
  height: 50px;
  background: var(--paper-txt);
}
.r-barcode-bar.thin { width: 1px; }
.r-barcode-bar.space { background: transparent; }
.r-barcode-label {
  font-size: 10px;
  font-family: 'Courier New', monospace;
  letter-spacing: 2px;
}

.r-qr {
  text-align: center;
  padding: 12px 0;
}
.r-qr-grid {
  display: inline-grid;
  gap: 0;
  border: 4px solid var(--paper-txt);
  padding: 4px;
  background: #fff;
}
.r-qr-cell {
  width: 4px;
  height: 4px;
}
.r-qr-cell.dark { background: var(--paper-txt); }
.r-qr-cell.light { background: #fff; }
.r-qr-label {
  font-size: 9px;
  color: #888;
  margin-top: 4px;
}

.r-cut {
  text-align: center;
  margin: 8px 0;
  border-top: 2px dashed #ccc;
  padding-top: 4px;
  font-size: 10px;
  color: #bbb;
  letter-spacing: 1px;
}

.r-blank { height: 1.6em; }

/* ── Empty state ── */
.empty-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 16px;
  padding: 60px 20px;
  color: var(--muted);
  text-align: center;
}

.empty-state p { font-size: 13px; line-height: 1.6; }
.empty-state strong { color: var(--text); }

/* ── Code output ── */
.code-wrapper {
  flex: 1;
  overflow: auto;
  padding: 20px;
  background: var(--bg);
}

.code-output {
  font-family: 'SF Mono', 'Fira Code', 'Courier New', monospace;
  font-size: 12px;
  line-height: 1.6;
  color: var(--blue);
  white-space: pre;
  tab-size: 2;
}

/* ── Hex dump ── */
.hex-wrapper {
  flex: 1;
  overflow: auto;
  padding: 16px 20px;
  background: var(--bg);
}

.hex-dump {
  font-family: 'SF Mono', 'Fira Code', monospace;
  font-size: 11px;
  color: var(--green);
  white-space: pre-wrap;
  word-break: break-all;
}

/* ── Stats bar ── */
.stats-bar {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 8px 20px;
  background: var(--surface);
  border-top: 1px solid var(--border);
  font-size: 12px;
  color: var(--muted);
  flex-shrink: 0;
}

.stat-sep { color: var(--border); font-size: 10px; }

/* ── Toast ── */
.toast {
  position: fixed;
  bottom: 24px;
  left: 50%;
  transform: translateX(-50%) translateY(80px);
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--text);
  padding: 10px 18px;
  font-size: 13px;
  transition: transform 0.25s ease, opacity 0.25s ease;
  opacity: 0;
  pointer-events: none;
  z-index: 999;
  white-space: nowrap;
}

.toast.show {
  transform: translateX(-50%) translateY(0);
  opacity: 1;
}

/* ── Scrollbar ── */
::-webkit-scrollbar { width: 6px; height: 6px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--border); border-radius: 3px; }

/* ── Logo upload ── */
.logo-upload-area {
  background: var(--bg);
  border: 1px dashed var(--border);
  border-radius: var(--radius);
  padding: 12px;
  text-align: center;
}

.logo-preview {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
}

.btn-upload {
  display: flex;
  align-items: center;
  gap: 6px;
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--muted);
  cursor: pointer;
  font-size: 12px;
  padding: 7px 14px;
  transition: color 0.15s, border-color 0.15s;
}
.btn-upload:hover { color: var(--text); border-color: var(--text); }

.btn-remove-logo {
  background: none;
  border: 1px solid var(--border);
  color: var(--muted);
  cursor: pointer;
  font-size: 14px;
  padding: 2px 8px;
  border-radius: 4px;
  transition: color 0.15s, border-color 0.15s;
}
.btn-remove-logo:hover { color: var(--accent); border-color: var(--accent); }

/* ── Print button highlight ── */
.btn-print {
  background: linear-gradient(135deg, var(--accent), #d63851) !important;
  color: #fff !important;
  border-color: var(--accent) !important;
  font-weight: 600;
}
.btn-print:hover:not(:disabled) { opacity: 0.9; }
.btn-print:disabled { opacity: 0.4; }

/* ── JSON toolbar ── */
.json-toolbar {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 0 0 12px;
  border-bottom: 1px solid var(--border);
  margin-bottom: 12px;
}

.json-hint {
  font-size: 11px;
  color: var(--muted);
}

.json-hint code {
  background: var(--surface2);
  padding: 2px 6px;
  border-radius: 3px;
  font-family: 'SF Mono', 'Fira Code', monospace;
  font-size: 11px;
  color: var(--green);
}

.json-output {
  color: var(--accent2) !important;
}

/* ── Responsive ── */
@media (max-width: 768px) {
  body { overflow: auto; }
  .app {
    grid-template-columns: 1fr;
    grid-template-rows: auto 1fr;
    height: auto;
  }
  .sidebar { max-height: 60dvh; }
  .preview-pane { min-height: 80dvh; }
  #receipt-form { padding-bottom: 20px; }
  .btn-build { position: static; width: 100%; margin: 12px 0 0; }
  .toolbar-actions { flex-wrap: wrap; }
}
