:root {
  --bg-primary: #f2f2f7;
  --bg-secondary: rgba(255, 255, 255, 0.72);
  --bg-tertiary: rgba(255, 255, 255, 0.95);
  --bg-table: rgba(255, 255, 255, 0.8);
  --bg-table-header: rgba(255, 255, 255, 0.6);
  --bg-row-even: #f5f5f7;
  --bg-row-active: rgba(10, 132, 255, 0.15);
  --bg-toggle: rgba(255, 255, 255, 0.6);
  --bg-input: rgba(255, 255, 255, 0.9);

  --text-primary: rgba(0, 0, 0, 0.85);
  --text-secondary: rgba(0, 0, 0, 0.5);
  --text-header: rgba(0, 0, 0, 0.9);
  --text-table-header: rgba(0, 0, 0, 0.6);
  --text-toggle: rgba(0, 0, 0, 0.7);
  --text-accent: #007aff;

  --border-primary: rgba(255, 255, 255, 0.3);
  --border-secondary: rgba(0, 0, 0, 0.15);
  --border-table: rgba(0, 0, 0, 0.1);
  --border-input: rgba(0, 0, 0, 0.15);

  --shadow-light: rgba(0, 0, 0, 0.1);
}

[data-theme="dark"] {
  --bg-primary: #000000;
  --bg-secondary: rgba(28, 28, 30, 0.72);
  --bg-tertiary: rgba(28, 28, 30, 0.95);
  --bg-table: rgba(28, 28, 30, 0.8);
  --bg-table-header: rgba(28, 28, 30, 0.6);
  --bg-row-even: #1c1c1e;
  --bg-row-active: rgba(10, 132, 255, 0.25);
  --bg-toggle: rgba(28, 28, 30, 0.6);
  --bg-input: rgba(28, 28, 30, 0.9);

  --text-primary: rgba(255, 255, 255, 0.85);
  --text-secondary: rgba(255, 255, 255, 0.5);
  --text-header: rgba(255, 255, 255, 0.9);
  --text-table-header: rgba(255, 255, 255, 0.6);
  --text-toggle: rgba(255, 255, 255, 0.7);
  --text-accent: #0a84ff;

  --border-primary: rgba(255, 255, 255, 0.3);
  --border-secondary: rgba(255, 255, 255, 0.15);
  --border-table: rgba(255, 255, 255, 0.1);
  --border-input: rgba(255, 255, 255, 0.15);

  --shadow-light: rgba(0, 0, 0, 0.3);
}

/* RESET */
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

/* BODY */
body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto;
  background: var(--bg-primary);
  padding: 24px;
  color: var(--text-primary);
  transition: background-color 0.3s ease, color 0.3s ease;
  min-height: 100vh;
}

/* HEADER */
.header {
  text-align: center;
  margin-bottom: 20px;
}

.header h1 {
  font-size: 26px;
  color: var(--text-header);
  font-weight: 700;
  letter-spacing: -0.5px;
  transition: color 0.3s ease;
}

.title-time {
  margin-top: 8px;
  font-size: 12px;
  color: var(--text-secondary);
  text-align: center;
  transition: color 0.3s ease;
}

/* CONTAINER */
.app-container {
  width: 100%;
  max-width: 450px;
  margin: auto;
  background: var(--bg-secondary);
  border: 1px solid var(--border-primary);
  border-radius: 24px;
  padding: 24px;
  box-shadow: 0 10px 30px var(--shadow-light);
  backdrop-filter: blur(20px);
  -webkit-backdrop-filter: blur(20px);
  transition: all 0.3s ease;
}

.dashboard-layout {
  display: flex;
  flex-direction: column;
  gap: 20px;
}

/* ACTIONS TOP */
.actions-top {
  display: flex;
  gap: 8px;
  margin-top: 8px;
  margin-bottom: 16px;
  flex-direction: row;
}

.btn-action-top {
  flex: 1;
  min-width: 0;
  padding: 12px 14px;
  border: 1px solid var(--border-secondary);
  border-radius: 12px;
  font-weight: 600;
  font-size: 13px;
  cursor: pointer;
  transition: all 0.18s ease;
  color: var(--text-accent);
  background: var(--bg-tertiary);
}

.btn-action-top:hover {
  border-color: var(--text-accent);
  background: var(--bg-row-hover);
  transform: translateY(-1px);
}

.btn-action-top:active {
  opacity: 0.95;
  transform: translateY(0);
}

/* GRID TABLE */
.denoms-grid {
  border-radius: 16px;
  overflow: hidden;
  border: 1px solid var(--border-table);
  background: var(--bg-table);
  box-shadow: 0 4px 12px var(--shadow-medium);
  transition: background-color 0.3s ease, border-color 0.3s ease;
}

/* HEADER */
.grid-header {
  display: grid;
  grid-template-columns: 1fr 140px 1fr;
  background: var(--bg-table-header);
  font-size: 12px;
  text-transform: uppercase;
  color: var(--text-table-header);
  font-weight: 700;
  transition: background-color 0.3s ease, color 0.3s ease;
}

.grid-header div {
  padding: 14px;
}

/* ROW */
.grid-row {
  display: grid;
  grid-template-columns: 1fr 140px 1fr;
  border-top: 1px solid var(--border-table);
  align-items: center;
  transition: background-color 0.3s ease;
}

.grid-row div {
  padding: 14px 16px;
  color: var(--text-primary);
  transition: color 0.3s ease;
}

.grid-row:nth-child(odd) {
  background: var(--bg-tertiary);
}

.grid-row:nth-child(even) {
  background: var(--bg-row-even);
}

.grid-row.row-active {
  background: var(--bg-row-active) !important;
}

.grid-row.row-active div {
  background: transparent !important;
}

/* ALIGN */
.grid-row div:nth-child(2) {
  text-align: center;
}

.grid-row div:nth-child(3) {
  text-align: right;
  font-weight: 600;
  color: var(--text-primary);
  transition: color 0.3s ease;
}

/* QTY */
.qty-cell {
  display: inline-flex;
  justify-content: center;
  align-items: center;
  background: var(--bg-tertiary);
  border: 1px solid var(--border-input);
  border-radius: 20px;
  padding: 2px;
  transition: border-color 0.3s ease, box-shadow 0.3s ease;
}

.qty-cell:focus-within {
  border-color: var(--text-accent);
  box-shadow: 0 0 0 3px rgba(10, 132, 255, 0.2);
}

.qty-input {
  width: 50px;
  text-align: center;
  padding: 6px 0;
  border: none;
  font-size: 16px;
  font-weight: 600;
  color: var(--text-primary);
  outline: none;
  background: transparent;
  transition: color 0.3s ease;
}

/* Remove spinner for number inputs (WebKit/Chromium) */
.qty-input::-webkit-outer-spin-button,
.qty-input::-webkit-inner-spin-button {
  -webkit-appearance: none;
  appearance: none;
  margin: 0;
}
.qty-input {
  -moz-appearance: textfield;
  appearance: textfield;
}

.btn-qty {
  width: 34px;
  height: 34px;
  border-radius: 50%;
  border: none;
  background: transparent;
  color: var(--text-accent);
  font-weight: 500;
  font-size: 20px;
  cursor: pointer;
  transition: all 0.2s;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0;
  touch-action: manipulation;
  user-select: none;
}

.btn-qty:hover {
  background: rgba(10, 132, 255, 0.1);
}

.btn-qty:active {
  background: rgba(10, 132, 255, 0.2);
  transform: scale(0.92);
}

/* RESULT */
.result {
  background: var(--bg-tertiary);
  color: var(--text-primary);
  padding: 20px;
  border: 1px solid var(--border-table);
  border-radius: 14px;
  margin: 0 0 8px;
  transition: background-color 0.3s ease, color 0.3s ease, border-color 0.3s ease;
}

.total-amount {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 12px;
}

.total-amount .label {
  font-weight: 600;
  font-size: 14px;
}

.total-amount .amount {
  font-size: 22px;
  font-weight: 700;
  color: var(--text-accent);
  transition: color 0.3s ease;
}

.total-words {
  display: flex;
  justify-content: flex-end;
  align-items: flex-start;
}

.total-words .words {
  font-size: 16px;
  line-height: 1.4;
  text-align: right;
  color: #86868b;
  transition: color 0.3s ease;
}

.hidden {
  display: none;
}

/* Toggle Switch */
.toggle-small {
  display: flex;
  align-items: center;
  gap: 16px;
  justify-content: flex-end;
  margin: 0;
  padding: 14px 16px;
  background: var(--bg-toggle);
  border: 1px solid var(--border-table);
  border-radius: 0 0 16px 16px;
  transition: background-color 0.3s ease, border-color 0.3s ease;
}

.toggle-label {
  font-weight: 600;
  color: var(--text-toggle);
  font-size: 14px;
  transition: color 0.3s ease;
}

.switch {
  position: relative;
  display: inline-block;
  width: 50px;
  height: 28px;
}

.switch input {
  opacity: 0;
  width: 0;
  height: 0;
}

.slider {
  position: absolute;
  cursor: pointer;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  width: 50px;
  height: 28px;
  background-color: var(--border-secondary);
  transition: 0.3s;
  border-radius: 34px;
}

.slider:before {
  position: absolute;
  content: "";
  height: 22px;
  width: 22px;
  left: 3px;
  bottom: 3px;
  background-color: white;
  transition: 0.3s;
  border-radius: 50%;
}

input:checked + .slider {
  background-color: var(--text-accent);
}

input:checked + .slider:before {
  transform: translateX(22px);
}

/* Toast Notification */
.toast {
  position: fixed;
  bottom: 20px;
  right: 20px;
  background: var(--bg-tertiary);
  color: var(--text-primary);
  padding: 16px 24px;
  border-radius: 8px;
  box-shadow: 0 8px 20px var(--shadow-light);
  font-weight: 500;
  font-size: 14px;
  animation: slideInUp 0.3s ease-out, fadeOut 0.5s ease-out 2.5s forwards;
  z-index: 1000;
  display: flex;
  align-items: center;
  gap: 10px;
  transition: background-color 0.3s ease, color 0.3s ease;
}

.toast.success {
  border-left: 4px solid var(--text-accent);
}

.toast.error {
  border-left: 4px solid var(--text-accent);
}

.toast-icon {
  font-size: 18px;
  font-weight: bold;
}

.toast.success .toast-icon {
  color: var(--text-accent);
}

.toast.error .toast-icon {
  color: var(--text-accent);
}

/* MOBILE */
@media (max-width: 480px) {
  .container {
    padding: 18px 14px;
    border-radius: 16px;
  }

  .header h1 {
    font-size: 24px;
  }

  .actions-top {
    gap: 6px;
  }

  .btn-action-top {
    min-width: 0;
    border-radius: 10px;
    padding: 10px 8px;
    font-size: 12px;
  }

  .grid-header div,
  .grid-row div {
    padding: 12px 7px;
    font-size: 13px;
  }

  .qty-cell {
    /* No gap needed for the new pill design */
  }

  .qty-input {
    width: 44px;
    font-size: 15px;
    padding: 6px 0;
  }

  .btn-qty {
    width: 32px;
    height: 32px;
    font-size: 18px;
  }

  .toast {
    bottom: 10px;
    right: 10px;
    left: 10px;
  }
}

/* POS TERMINALS / ULTRA NARROW SCREENS (e.g., 276px width) */
@media (max-width: 320px) {
  /* Performance Optimization for weak hardware */
  *, *::before, *::after {
    transition: none !important;
    backdrop-filter: none !important;
    -webkit-backdrop-filter: none !important;
    box-shadow: none !important;
  }
  
  body {
    padding: 8px 6px;
  }
  .app-container {
    padding: 12px 8px;
    border-radius: 12px;
  }
  .header h1 {
    font-size: 20px;
  }
  .grid-header {
    grid-template-columns: 75px 65px 1fr;
  }
  .grid-row {
    grid-template-columns: 75px 65px 1fr;
  }
  .grid-header div,
  .grid-row div {
    padding: 10px 4px;
    font-size: 11px;
  }
  
  /* Disable +/- buttons entirely, use only input */
  .btn-qty {
    display: none !important;
  }
  .qty-cell {
    background: transparent !important;
    border: none !important;
    padding: 0 !important;
  }
  .qty-input {
    width: 100%;
    font-size: 14px;
    padding: 6px 4px;
    border: 1px solid var(--border-input);
    border-radius: 6px;
    background: var(--bg-input);
  }
  
  /* Hide the receipt view, only show total */
  .receipt-header,
  .receipt-divider,
  .receipt-body {
    display: none !important;
  }
  .receipt-ticket {
    background: transparent !important;
    border: none !important;
    box-shadow: none !important;
    padding: 0 !important;
    margin-bottom: 12px;
    border-radius: 0 !important;
    overflow: visible !important;
  }
  
  .actions-top {
    flex-wrap: wrap;
    gap: 4px;
  }
  .btn-action-top {
    padding: 8px 4px;
    font-size: 11px;
    flex: 1 1 30%;
  }
  
  /* Fix overflow for total amount and words on ultra-narrow screens */
  .receipt-row.total-amount {
    flex-direction: column;
    align-items: flex-end;
    gap: 4px;
  }
  .receipt-row.total-amount span:first-child {
    font-size: 12px;
    align-self: flex-start;
    color: var(--text-secondary);
  }
  .total-amount .amount {
    font-size: 18px;
    word-break: break-word;
  }
  .total-words {
    width: 100%;
  }
  .total-words .words {
    font-size: 11px;
    line-height: 1.5;
    word-break: break-word;
  }
}

/* Export/print:  hide all functional buttons */
body.is-exporting .actions-top,
body.is-exporting .btn-qty,
body.is-exporting .toggle-small {
  display: none !important;
}

body.is-exporting {
  color-scheme: light !important;
}

body.is-exporting,
body.is-exporting .container,
body.is-exporting .denoms-grid,
body.is-exporting .grid-header,
body.is-exporting .grid-row,
body.is-exporting .grid-header div,
body.is-exporting .grid-row div,
body.is-exporting .result,
body.is-exporting .qty-input,
body.is-exporting .btn-action-top {
  background: #ffffff !important;
  color: #1c1c1e !important;
  border-color: #e5e5ea !important;
}

body.is-exporting .grid-header {
  background: #f5f5f7 !important;
  color: #6b7280 !important;
}

body.is-exporting .header h1 {
  color: #000000 !important;
}

body.is-exporting .title-time {
  color: #8e8e93 !important;
}

body.is-exporting .total-amount .amount {
  color: #0a84ff !important;
}

body.is-exporting .total-words .words {
  color: #8e8e93 !important;
}

body.is-exporting .qty-input,
body.is-exporting .qty-input:focus {
  box-shadow: none !important;
  border-color: #cbd5e1 !important;
  background: #ffffff !important;
  -webkit-appearance: none !important;
  appearance: none !important;
}

body.is-exporting .row-active {
  background: transparent !important;
}

body.is-exporting .row-active div {
  background: transparent !important;
}

@media print {
  @page {
    margin: 0; /* Remove headers/footers for receipt printers */
  }

  :root {
    color-scheme: light !important;
  }

  .actions-top,
  .btn-qty,
  .toggle-small,
  .header,
  .left-panel {
    display: none !important;
  }

  .app-container, .dashboard-layout, .right-panel, .receipt-ticket {
    display: block !important;
    width: 100% !important;
    max-width: 100% !important;
    margin: 0 !important;
    padding: 0 !important;
    border: none !important;
    box-shadow: none !important;
    border-radius: 0 !important;
  }

  body,
  .receipt-ticket,
  .receipt-inner,
  .receipt-body,
  .receipt-item-name,
  .receipt-item-total,
  .receipt-header h2 {
    background: #ffffff !important;
    color: #000000 !important;
  }

  .receipt-timestamp {
    color: #333333 !important;
  }

  .total-amount .amount {
    color: #000000 !important;
    font-size: 18px !important;
  }

  .total-words .words {
    color: #000000 !important;
  }

  .qty-input,
  .qty-input:focus {
    box-shadow: none !important;
    border-color: #cbd5e1 !important;
    background: #ffffff !important;
    -webkit-appearance: none !important;
    appearance: none !important;
  }

  .row-active {
    background: transparent !important;
  }

  .row-active div {
    background: transparent !important;
  }

  body {
    padding: 0;
  }

  .container {
    box-shadow: none;
    border-radius: 0;
    max-width: 100%;
  }
}

/* Modal */
.modal {
  display: flex;
  position: fixed;
  z-index: 2000;
  left: 0;
  top: 0;
  width: 100%;
  height: 100%;
  background-color: rgba(0, 0, 0, 0.4);
  backdrop-filter: blur(5px);
  -webkit-backdrop-filter: blur(5px);
  align-items: center;
  justify-content: center;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.25s ease;
}

.modal.show {
  opacity: 1;
  pointer-events: auto;
}

.modal-content {
  background: var(--bg-tertiary);
  color: var(--text-primary);
  padding: 24px;
  border-radius: 20px;
  max-width: 90%;
  width: 400px;
  position: relative;
  box-shadow: 0 10px 30px var(--shadow-light);
  border: 1px solid var(--border-primary);
  transform: scale(0.9);
  transition: transform 0.25s ease;
}

.modal.show .modal-content {
  transform: scale(1);
}

.modal-close {
  position: absolute;
  right: 18px;
  top: 14px;
  font-size: 24px;
  font-weight: bold;
  cursor: pointer;
  color: var(--text-secondary);
}

.modal-close:hover {
  color: var(--text-primary);
}

.modal-content h2 {
  font-size: 18px;
  font-weight: 700;
  margin-bottom: 20px;
  color: var(--text-header);
}

.modal-body {
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.instruction-step {
  display: flex;
  gap: 12px;
  align-items: flex-start;
}

.step-icon {
  font-size: 18px;
  line-height: 1.4;
}

.step-text strong {
  font-size: 14px;
  color: var(--text-primary);
  display: block;
  margin-bottom: 4px;
}

.step-text p {
  font-size: 13px;
  color: var(--text-secondary);
  line-height: 1.4;
}

body.is-exporting .modal {
  display: none !important;
}

@media print {
  .modal {
    display: none !important;
  }
}

/* Receipt Ticket styles */
.receipt-ticket {
  background: #ffffff;
  color: #1d1d1f;
  border-radius: 16px;
  padding: 24px;
  box-shadow: 0 10px 25px rgba(0, 0, 0, 0.08);
  font-family: 'SF Mono', Monaco, Consolas, 'Courier New', monospace;
  position: relative;
  overflow: hidden;
  border: 1px solid rgba(0, 0, 0, 0.06);
  margin-bottom: 20px;
}

.receipt-inner {
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.receipt-header {
  text-align: center;
}

.receipt-header h2 {
  font-size: 16px;
  font-weight: 700;
  letter-spacing: 1px;
  color: #1d1d1f;
  margin-bottom: 6px;
}

.receipt-timestamp {
  font-size: 11px;
  color: #86868b;
}

.receipt-divider {
  border-top: 1px dashed #c7c7cc;
  margin: 4px 0;
}

.receipt-body {
  display: flex;
  flex-direction: column;
  gap: 10px;
  min-height: 80px;
  max-height: 280px;
  overflow-y: auto;
}

.receipt-item {
  display: flex;
  justify-content: space-between;
  font-size: 13px;
  line-height: 1.4;
}

.receipt-item-name {
  color: #3a3a3c;
}

.receipt-item-total {
  font-weight: 600;
  color: #1d1d1f;
}

.receipt-empty {
  text-align: center;
  color: #86868b;
  font-style: italic;
  font-size: 13px;
  margin: auto;
}

.receipt-footer {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.receipt-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.receipt-row.total-amount {
  font-weight: 700;
  font-size: 15px;
  color: #1d1d1f;
}

.receipt-row.total-amount .amount {
  font-size: 18px;
  color: #007aff;
}

.receipt-words {
  font-size: 11px;
  color: #86868b;
  text-align: right;
  line-height: 1.4;
  font-style: italic;
  margin-top: 4px;
}

/* Scrollbar in receipt body */
.receipt-body::-webkit-scrollbar {
  width: 4px;
}
.receipt-body::-webkit-scrollbar-thumb {
  background: #cbd5e0;
  border-radius: 2px;
}

/* Responsive Desktop styles */
@media (min-width: 768px) {
  body {
    padding: 40px 20px;
  }

  .app-container {
    max-width: 860px;
    width: 100%;
    border-radius: 28px;
    padding: 32px;
  }

  .header {
    text-align: left;
    margin-bottom: 28px;
  }

  .header h1 {
    font-size: 32px;
  }

  .title-time {
    text-align: left;
    margin-top: 4px;
  }

  .dashboard-layout {
    display: flex;
    flex-direction: row;
    gap: 32px;
    align-items: flex-start;
  }

  .left-panel {
    flex: 1;
    display: flex;
    flex-direction: column;
    gap: 16px;
  }

  .right-panel {
    width: 320px;
    flex-shrink: 0;
    display: flex;
    flex-direction: column;
    position: sticky;
    top: 24px;
  }

  .receipt-ticket {
    margin-bottom: 24px;
  }
}