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

:root {
  --bg: #f5f5f5;
  --surface: #ffffff;
  --surface2: #eeeeee;
  --text: #1a1a1a;
  --text2: #666666;
  --border: #e0e0e0;
  --accent: #0f3460;
  --accent2: #1657a0;
  --green: #4caf50;
  --red: #e53935;
  --orange: #ff9800;
  --shadow: 0 2px 8px rgba(0,0,0,0.08);
  --shadow-lg: 0 4px 16px rgba(0,0,0,0.12);
  --radius: 12px;
  --nav-height: 64px;
  --header-height: 56px;
  --text-scale: 1;
}

[data-text-size="xsmall"] { --text-scale: 0.7; }
[data-text-size="small"] { --text-scale: 0.85; }
[data-text-size="medium"] { --text-scale: 1; }
[data-text-size="large"] { --text-scale: 1.2; }
[data-text-size="xlarge"] { --text-scale: 1.5; }

[data-theme="dark"] {
  --bg: #121212;
  --surface: #1e1e1e;
  --surface2: #2a2a2a;
  --text: #e0e0e0;
  --text2: #999999;
  --border: #333333;
  --accent: #4da6ff;
  --accent2: #80c0ff;
  --shadow: 0 2px 8px rgba(0,0,0,0.3);
  --shadow-lg: 0 4px 16px rgba(0,0,0,0.4);
}

html, body {
  height: 100%;
  overflow: hidden;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  display: flex;
  flex-direction: column;
  user-select: none;
  -webkit-tap-highlight-color: transparent;
  font-size: calc(16px * var(--text-scale));
}

header {
  height: var(--header-height);
  background: var(--accent);
  color: white;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 16px;
  flex-shrink: 0;
  z-index: 10;
}

header h1 {
  font-size: 1.25em;
  font-weight: 600;
  letter-spacing: 0.5px;
  flex: 1;
  text-align: center;
}

#backBtn {
  display: none;
  width: 36px;
  height: 36px;
  background: rgba(255,255,255,0.15);
  border: none;
  color: white;
  border-radius: 50%;
  cursor: pointer;
  font-size: 1.125em;
  align-items: center;
  justify-content: center;
  transition: background 0.2s;
  flex-shrink: 0;
}

#backBtn:hover {
  background: rgba(255,255,255,0.25);
}

main {
  flex: 1;
  overflow-y: auto;
  padding: 16px;
  padding-bottom: calc(var(--nav-height) + 16px);
  -webkit-overflow-scrolling: touch;
  scrollbar-width: none;
  -ms-overflow-style: none;
}

.view {
  display: none;
}

.view.active {
  display: block;
}

/* Bottom Navigation */
nav#bottomNav {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  height: var(--nav-height);
  background: var(--surface);
  border-top: 1px solid var(--border);
  display: flex;
  z-index: 100;
  box-shadow: 0 -2px 8px rgba(0,0,0,0.08);
}

.nav-tab {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 4px;
  border: none;
  background: none;
  color: var(--text2);
  cursor: pointer;
  font-size: 0.75em;
  transition: color 0.2s;
  padding: 8px 0;
}

.nav-tab.active {
  color: var(--accent);
}

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

/* Store Cards */
.store-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 12px;
}

.store-card {
  background: var(--surface);
  border-radius: var(--radius);
  padding: 20px 12px;
  text-align: center;
  cursor: pointer;
  box-shadow: var(--shadow);
  transition: transform 0.15s, box-shadow 0.15s;
  position: relative;
  overflow: hidden;
  border: 1px solid var(--border);
}

.store-card:active {
  transform: scale(0.97);
  box-shadow: var(--shadow-lg);
}

.store-card-dragging {
  opacity: 0.3;
  transform: scale(0.95);
  transition: transform 0.15s;
}

.store-card .store-icon {
  width: 56px;
  height: 56px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  margin: 0 auto 12px;
  font-size: 0.875em;
  font-weight: 700;
  color: white;
  letter-spacing: 1px;
}

.store-card .store-name {
  font-size: 0.9375em;
  font-weight: 600;
}

.store-card .store-items {
  font-size: 0.75em;
  color: var(--text2);
  margin-top: 4px;
}

/* Products page */
.products-header {
  display: flex;
  gap: 8px;
  margin-bottom: 16px;
}

.products-header input {
  flex: 1;
  min-width: 0;
  padding: 12px 16px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  font-size: 0.9375em;
  background: var(--surface);
  color: var(--text);
  outline: none;
}

.products-header input:focus {
  border-color: var(--accent);
}

.products-header button {
  padding: 12px 16px;
  background: var(--accent);
  color: white;
  border: none;
  border-radius: var(--radius);
  font-size: 0.9375em;
  font-weight: 600;
  cursor: pointer;
  white-space: nowrap;
  flex-shrink: 0;
}

.product-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  background: var(--surface);
  padding: 14px 16px;
  border-radius: var(--radius);
  margin-bottom: 8px;
  border: 1px solid var(--border);
  box-shadow: var(--shadow);
}

.product-item span {
  font-size: 0.9375em;
  font-weight: 500;
}

.product-item button {
  background: none;
  border: none;
  color: var(--red);
  font-size: 1.25em;
  cursor: pointer;
  padding: 4px 8px;
  border-radius: 6px;
}

.product-item button:hover {
  background: rgba(229,57,53,0.1);
}

.empty-state {
  text-align: center;
  color: var(--text2);
  padding: 48px 16px;
  font-size: 0.9375em;
}

.empty-state svg {
  display: block;
  margin: 0 auto 12px;
  opacity: 0.3;
}

/* Shopping list */
.shopping-item {
  display: flex;
  align-items: center;
  gap: 12px;
  background: var(--surface);
  padding: 14px 16px;
  border-radius: var(--radius);
  margin-bottom: 8px;
  border: 1px solid var(--border);
  box-shadow: var(--shadow);
  cursor: pointer;
  transition: background 0.15s;
}

.shopping-item:active {
  background: var(--surface2);
}

.shopping-item.bought {
  opacity: 0.55;
}

.shopping-item.bought span {
  text-decoration: line-through;
  color: var(--text2);
}

.shopping-item .checkbox {
  width: 24px;
  height: 24px;
  border: 2px solid var(--border);
  border-radius: 6px;
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all 0.2s;
}

.shopping-item.bought .checkbox {
  background: var(--green);
  border-color: var(--green);
}

.shopping-item .checkbox::after {
  content: '';
  width: 6px;
  height: 12px;
  border: solid white;
  border-width: 0 2px 2px 0;
  transform: rotate(45deg);
  display: none;
}

.shopping-item.bought .checkbox::after {
  display: block;
}

.shopping-item span {
  flex: 1;
  font-size: 0.9375em;
  font-weight: 500;
}

.shopping-item .item-delete {
  background: none;
  border: none;
  color: var(--red);
  font-size: 1.125em;
  font-weight: 300;
  cursor: pointer;
  padding: 4px 8px;
  border-radius: 6px;
  flex-shrink: 0;
  line-height: 1;
}

.shopping-item .item-delete:hover {
  background: rgba(229,57,53,0.08);
}

.shopping-actions {
  display: flex;
  gap: 8px;
  margin-top: 16px;
}

.btn {
  padding: 10px 20px;
  border: none;
  border-radius: var(--radius);
  font-size: 0.875em;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.2s;
}

.btn-secondary {
  background: var(--surface2);
  color: var(--text);
}

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

/* Bon tab placeholder */
.placeholder-view {
  text-align: center;
  padding: 64px 16px;
  color: var(--text2);
}

.placeholder-view svg {
  display: block;
  margin: 0 auto 16px;
  opacity: 0.25;
}

.placeholder-view p {
  font-size: 0.9375em;
}

/* Add all products button */
.add-all-btn {
  width: 100%;
  margin-top: 12px;
}

main::-webkit-scrollbar {
  display: none;
}

/* Modal */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.5);
  z-index: 200;
  display: flex;
  align-items: flex-end;
  justify-content: center;
  animation: fadeIn 0.2s;
}

.modal-sheet {
  background: var(--surface);
  border-radius: 16px 16px 0 0;
  width: 100%;
  max-width: 500px;
  max-height: 70vh;
  display: flex;
  flex-direction: column;
  animation: slideUp 0.25s ease-out;
}

.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px;
  border-bottom: 1px solid var(--border);
}

.modal-header h3 {
  font-size: 1.0625em;
  font-weight: 600;
}

.modal-close {
  background: none;
  border: none;
  font-size: 1.375em;
  color: var(--text2);
  cursor: pointer;
  padding: 4px 8px;
  border-radius: 6px;
}

.modal-body {
  overflow-y: auto;
  padding: 8px 16px;
  flex: 1;
}

.modal-footer {
  padding: 12px 16px;
  border-top: 1px solid var(--border);
}

.modal-footer .btn {
  width: 100%;
  background: var(--accent);
  color: white;
  font-size: 0.9375em;
  padding: 14px;
}

.modal-footer .btn:disabled {
  opacity: 0.4;
  cursor: default;
}

.modal-product-item {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px 0;
  border-bottom: 1px solid var(--border);
  cursor: pointer;
}

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

.modal-product-item .modal-checkbox {
  width: 22px;
  height: 22px;
  border: 2px solid var(--border);
  border-radius: 6px;
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: center;
}

.modal-product-item.selected .modal-checkbox {
  background: var(--accent);
  border-color: var(--accent);
}

.modal-product-item.selected .modal-checkbox::after {
  content: '';
  width: 5px;
  height: 10px;
  border: solid white;
  border-width: 0 2px 2px 0;
  transform: rotate(45deg);
}

.modal-product-item span {
  font-size: 0.9375em;
  font-weight: 500;
}

@keyframes fadeIn {
  from { opacity: 0; }
  to { opacity: 1; }
}

@keyframes slideUp {
  from { transform: translateY(100%); }
  to { transform: translateY(0); }
}

/* Settings */
.settings-section {
  background: var(--surface);
  border-radius: var(--radius);
  padding: 16px;
  margin-bottom: 12px;
  border: 1px solid var(--border);
  box-shadow: var(--shadow);
}

.settings-section h3 {
  font-size: 0.88em;
  font-weight: 600;
  color: var(--text2);
  text-transform: uppercase;
  letter-spacing: 0.5px;
  margin-bottom: 12px;
}

.settings-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 0;
}

.settings-row + .settings-row {
  border-top: 1px solid var(--border);
}

.settings-row .label {
  font-size: 0.94em;
  font-weight: 500;
}

.settings-row .hint {
  font-size: 0.8em;
  color: var(--text2);
  margin-top: 2px;
}

.settings-toggle {
  width: 52px;
  height: 30px;
  border-radius: 15px;
  border: none;
  background: var(--border);
  cursor: pointer;
  position: relative;
  transition: background 0.2s;
  flex-shrink: 0;
}

.settings-toggle.on {
  background: var(--accent);
}

.settings-toggle::after {
  content: '';
  position: absolute;
  top: 3px;
  left: 3px;
  width: 24px;
  height: 24px;
  border-radius: 50%;
  background: white;
  transition: transform 0.2s;
  box-shadow: 0 1px 3px rgba(0,0,0,0.2);
}

.settings-toggle.on::after {
  transform: translateX(22px);
}

.text-size-slider {
  width: 100%;
  -webkit-appearance: none;
  appearance: none;
  height: 6px;
  border-radius: 3px;
  background: var(--border);
  outline: none;
  margin: 8px 0 4px;
}
.text-size-slider::-webkit-slider-thumb {
  -webkit-appearance: none;
  appearance: none;
  width: 24px;
  height: 24px;
  border-radius: 50%;
  background: var(--accent);
  cursor: pointer;
  box-shadow: 0 1px 4px rgba(0,0,0,0.2);
}
.text-size-labels {
  display: flex;
  justify-content: space-between;
  font-size: 0.7em;
  color: var(--text2);
  padding: 0 2px;
  margin-top: 4px;
}
.text-size-preview {
  text-align: center;
  font-size: 1em;
  font-weight: 500;
  padding: 10px 0;
  opacity: 0.6;
}

/* Camera */
.camera-overlay {
  position: fixed;
  inset: 0;
  background: #000;
  z-index: 300;
  display: flex;
  flex-direction: column;
}
.camera-overlay video {
  flex: 1;
  width: 100%;
  object-fit: cover;
}
.camera-overlay canvas {
  display: none;
}
.camera-controls {
  display: flex;
  gap: 16px;
  justify-content: center;
  padding: 24px;
  background: rgba(0,0,0,0.8);
}
.camera-btn {
  width: 64px;
  height: 64px;
  border-radius: 50%;
  border: 4px solid white;
  background: rgba(255,255,255,0.2);
  cursor: pointer;
  transition: transform 0.15s;
}
.camera-btn:active {
  transform: scale(0.9);
}
.camera-close {
  position: absolute;
  top: 16px;
  right: 16px;
  width: 40px;
  height: 40px;
  border-radius: 50%;
  background: rgba(0,0,0,0.5);
  border: none;
  color: white;
  font-size: 1.375em;
  cursor: pointer;
  z-index: 10;
}

/* Bon items */
.bon-header {
  display: flex;
  gap: 8px;
  margin-bottom: 12px;
}
.bon-header button {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 12px 16px;
  border: none;
  border-radius: var(--radius);
  font-size: 0.88em;
  font-weight: 600;
  cursor: pointer;
  background: var(--surface2);
  color: var(--text);
  flex: 1;
  justify-content: center;
}
.bon-header .btn-camera {
  background: var(--accent);
  color: white;
}
.bon-item {
  display: flex;
  align-items: center;
  gap: 10px;
  background: var(--surface);
  padding: 12px 14px;
  border-radius: var(--radius);
  margin-bottom: 6px;
  border: 1px solid var(--border);
}
.bon-item .bon-info {
  flex: 1;
  min-width: 0;
}
.bon-item .bon-name {
  font-size: 0.88em;
  font-weight: 500;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.bon-item .bon-detail {
  font-size: 0.75em;
  color: var(--text2);
}
.bon-item .bon-subtotal {
  font-size: 0.88em;
  font-weight: 600;
  white-space: nowrap;
}
.bon-item .bon-delete {
  background: none;
  border: none;
  color: var(--red);
  font-size: 1em;
  cursor: pointer;
  padding: 4px 6px;
  border-radius: 4px;
}
.bon-total {
  background: var(--surface);
  border-radius: var(--radius);
  padding: 18px 20px;
  margin-top: 12px;
  border: 1px solid var(--border);
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 1.2em;
  font-weight: 700;
}
.bon-total .total-label {
  color: var(--text2);
  font-weight: 500;
}

/* Bon add form */
.bon-add-form {
  background: var(--surface);
  border-radius: var(--radius);
  padding: 16px;
  margin-bottom: 12px;
  border: 1px solid var(--border);
}
.bon-add-form .form-row {
  display: flex;
  gap: 8px;
  margin-bottom: 8px;
  flex-wrap: wrap;
}
.bon-add-form .form-row:last-child {
  margin-bottom: 0;
}
.bon-add-form input {
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: calc(var(--radius) - 4px);
  font-size: 0.88em;
  background: var(--bg);
  color: var(--text);
  outline: none;
}
.bon-add-form input:focus {
  border-color: var(--accent);
}
.bon-add-form .input-name { flex: 1 1 60%; min-width: 0; }
.bon-add-form .input-price { flex: 1 1 35%; min-width: 0; }
.bon-add-form .input-qty { width: 56px; text-align: center; }
.bon-add-form .btn-form-add {
  width: 100%;
  padding: 12px;
  background: var(--accent);
  color: white;
  border: none;
  border-radius: calc(var(--radius) - 4px);
  font-size: 0.88em;
  font-weight: 600;
  cursor: pointer;
  margin-top: 4px;
}
.bon-add-form .scanned-image {
  width: 100%;
  max-height: 140px;
  object-fit: contain;
  border-radius: 8px;
  background: #000;
  display: block;
}

/* Empty bon */
.bon-empty {
  text-align: center;
  padding: 48px 16px;
  color: var(--text2);
  font-size: 0.9375em;
}
.bon-empty svg {
  display: block;
  margin: 0 auto 12px;
  opacity: 0.25;
}

/* Toast */
.toast {
  position: fixed;
  bottom: 80px;
  right: 16px;
  background: var(--surface);
  color: var(--text);
  padding: 12px 20px;
  border-radius: 8px;
  font-size: 0.875em;
  font-weight: 500;
  box-shadow: 0 4px 16px rgba(0,0,0,0.2);
  z-index: 400;
  animation: toastIn 0.3s ease-out;
  border: 1px solid var(--border);
}
.toast-hide {
  animation: toastOut 0.3s ease-in forwards;
}
@keyframes toastIn {
  from { opacity: 0; transform: translateY(12px); }
  to { opacity: 1; transform: translateY(0); }
}
@keyframes toastOut {
  from { opacity: 1; }
  to { opacity: 0; }
}

/* Responsive */
@media (min-width: 600px) {
  .store-grid {
    grid-template-columns: repeat(3, 1fr);
  }
}

@media (min-width: 900px) {
  .store-grid {
    grid-template-columns: repeat(4, 1fr);
  }
}
