/* ===== RESET & BASE ===== */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --black: #000;
  --white: #fff;
  --gray-bg: #f5f5f5;
  --gray-line: #e8e8e8;
  --gray-text: #999;
  --accent: #000;
  --danger: #e53935;
  --green: #2e7d32;
  --radius: 10px;
  --header-h: 52px;
  --footer-h: 68px;
}

html, body {
  height: 100%;
  overflow: hidden;
  font-family: -apple-system, BlinkMacSystemFont, 'Hiragino Sans', 'Yu Gothic UI', sans-serif;
  font-size: 15px;
  background: var(--white);
  color: var(--black);
  -webkit-tap-highlight-color: transparent;
}

button { cursor: pointer; font-family: inherit; }

/* ===== APP LAYOUT ===== */
#app {
  display: flex;
  flex-direction: column;
  height: 100dvh;
  height: 100vh;
}

#app-header {
  flex-shrink: 0;
  height: var(--header-h);
  display: flex;
  align-items: center;
  padding: 0 14px;
  border-bottom: 1px solid var(--gray-line);
  background: var(--white);
  z-index: 10;
}

#menu-btn {
  background: none;
  border: none;
  font-size: 22px;
  padding: 6px;
  color: var(--black);
  line-height: 1;
}

#header-title {
  flex: 1;
  text-align: center;
  font-size: 17px;
  font-weight: 600;
}

.header-spacer { width: 34px; }

#input-screen {
  flex: 1;
  overflow-y: auto;
  padding: 0 16px 16px;
  -webkit-overflow-scrolling: touch;
}

#bottom-bar {
  flex-shrink: 0;
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 16px;
  padding-bottom: max(10px, env(safe-area-inset-bottom));
  border-top: 1px solid var(--gray-line);
  background: var(--white);
}

#undo-btn {
  flex: 1;
  height: 44px;
  border: 1px solid var(--gray-line);
  border-radius: var(--radius);
  background: var(--gray-bg);
  font-size: 15px;
  color: #333;
}

#output-btn {
  flex: 2;
  height: 44px;
  border: none;
  border-radius: var(--radius);
  background: var(--black);
  color: var(--white);
  font-size: 16px;
  font-weight: 600;
}

/* ===== SECTION HEADERS ===== */
.section-header {
  font-size: 11px;
  font-weight: 600;
  color: var(--gray-text);
  letter-spacing: 0.8px;
  text-transform: uppercase;
  padding: 14px 0 4px;
  border-bottom: 1px solid var(--gray-line);
}

/* ===== INPUT ROWS ===== */
.input-row {
  display: flex;
  align-items: center;
  min-height: 50px;
  padding: 6px 0;
  border-bottom: 1px solid var(--gray-line);
  gap: 8px;
}

.row-label {
  flex: 1;
  font-size: 15px;
  display: flex;
  align-items: center;
  gap: 4px;
  flex-wrap: wrap;
}

.ocr-badge {
  font-size: 10px;
  background: #e3f0ff;
  color: #1a6bcb;
  padding: 1px 5px;
  border-radius: 4px;
  font-weight: 600;
  letter-spacing: 0;
}

.value-wrap {
  display: flex;
  align-items: center;
  gap: 4px;
  flex-shrink: 0;
}

.input-chip {
  display: inline-block;
  min-width: 90px;
  text-align: right;
  padding: 6px 10px;
  background: var(--gray-bg);
  border: 1px solid var(--gray-line);
  border-radius: 8px;
  font-size: 16px;
  font-weight: 500;
  cursor: pointer;
  color: var(--black);
  transition: background 0.1s;
}

.input-chip:active { background: #e8e8e8; }

.input-chip.empty {
  color: var(--gray-text);
  font-size: 13px;
  font-weight: 400;
}

.input-chip.warning {
  color: var(--danger);
  border-color: var(--danger);
}

.unit {
  font-size: 14px;
  color: #555;
  min-width: 16px;
}

/* ===== DATE ROW ===== */
.date-display-wrap {
  display: flex;
  align-items: center;
  gap: 6px;
}

.icon-btn {
  background: none;
  border: none;
  font-size: 18px;
  padding: 4px;
  line-height: 1;
}

/* ===== MERGE STYLING ===== */
.merge-badge {
  font-size: 11px;
  background: #e8f5e9;
  color: var(--green);
  padding: 1px 6px;
  border-radius: 10px;
  font-weight: 600;
  display: none;
}

.merge-badge.visible { display: inline-block; }

.merge-badge button.remove-merge {
  background: none;
  border: none;
  color: var(--green);
  font-size: 12px;
  padding: 0 0 0 3px;
  line-height: 1;
  vertical-align: middle;
}

.merge-arrow-label {
  font-size: 11px;
  color: var(--gray-text);
  font-style: italic;
}

.input-row.merged-away {
  opacity: 0.4;
}

.input-row.drag-highlight {
  background: #e8f5e9;
  border-radius: 8px;
}

.input-row.drag-active {
  opacity: 0.5;
  background: #fff3e0;
}


/* ===== HAMBURGER MENU ===== */
#menu-overlay {
  position: fixed;
  inset: 0;
  z-index: 100;
}

#menu-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(0,0,0,0.35);
}

#menu-panel {
  position: absolute;
  top: 0;
  left: 0;
  width: 230px;
  height: 100%;
  background: var(--white);
  padding-top: 60px;
  box-shadow: 3px 0 12px rgba(0,0,0,0.12);
  display: flex;
  flex-direction: column;
}

.menu-item {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 17px 20px;
  font-size: 16px;
  border: none;
  background: none;
  color: var(--black);
  border-bottom: 1px solid var(--gray-line);
  text-align: left;
}

.menu-item:active { background: var(--gray-bg); }
.menu-icon { font-size: 20px; }


/* ===== CALCULATOR KEYBOARD ===== */
#keyboard-overlay {
  position: fixed;
  inset: 0;
  z-index: 200;
}

#keyboard-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(0,0,0,0.25);
}

#keyboard-panel {
  position: absolute;
  bottom: 0;
  left: 0;
  right: 0;
  background: var(--white);
  border-radius: 18px 18px 0 0;
  padding: 16px 16px 28px;
  box-shadow: 0 -4px 20px rgba(0,0,0,0.12);
}

#keyboard-field-label {
  font-size: 12px;
  color: var(--gray-text);
  margin-bottom: 8px;
}

#keyboard-display {
  text-align: right;
  padding: 10px 14px;
  background: var(--gray-bg);
  border-radius: 10px;
  margin-bottom: 12px;
  min-height: 52px;
  display: flex;
  align-items: center;
  justify-content: flex-end;
}

#keyboard-value {
  font-size: 34px;
  font-weight: 300;
  color: var(--black);
}

#keyboard-value.empty { color: var(--gray-text); }

.key-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
  margin-bottom: 8px;
}

.key {
  padding: 18px;
  font-size: 24px;
  font-weight: 400;
  background: #f0f0f0;
  border: none;
  border-radius: 10px;
  color: var(--black);
  transition: background 0.1s;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
}

.key:active { background: #d8d8d8; }

.key.key-fn {
  font-size: 18px;
  background: #e0e0e0;
  color: #333;
}

#key-confirm {
  width: 100%;
  padding: 16px;
  border: none;
  border-radius: 10px;
  background: var(--black);
  color: var(--white);
  font-size: 17px;
  font-weight: 600;
  margin-top: 2px;
}

#key-confirm:active { background: #333; }


/* ===== FULL SCREENS (preview, settings, ocr) ===== */
.screen-header {
  display: flex;
  align-items: center;
  height: var(--header-h);
  padding: 0 14px;
  border-bottom: 1px solid var(--gray-line);
  flex-shrink: 0;
}

.screen-header h2 {
  flex: 1;
  text-align: center;
  font-size: 17px;
  font-weight: 600;
}

.back-btn {
  background: none;
  border: none;
  font-size: 20px;
  color: var(--black);
  padding: 4px 8px 4px 0;
  width: 36px;
  text-align: left;
}

/* ===== PREVIEW SCREEN ===== */
#preview-screen {
  position: fixed;
  inset: 0;
  background: var(--white);
  z-index: 300;
  display: flex;
  flex-direction: column;
}

#preview-text {
  flex: 1;
  overflow-y: auto;
  padding: 20px 18px;
  font-size: 14px;
  line-height: 1.9;
  white-space: pre-wrap;
  font-family: 'Hiragino Sans', 'Yu Gothic', sans-serif;
  color: var(--black);
}

#copy-btn {
  flex-shrink: 0;
  margin: 12px 16px 0;
  margin-bottom: max(24px, env(safe-area-inset-bottom));
  padding: 15px;
  border: none;
  border-radius: var(--radius);
  background: var(--black);
  color: var(--white);
  font-size: 16px;
  font-weight: 600;
}

#copy-btn.copied {
  background: var(--green);
}


/* ===== SETTINGS SCREEN ===== */
#settings-screen {
  position: fixed;
  inset: 0;
  background: var(--white);
  z-index: 300;
  display: flex;
  flex-direction: column;
}

#settings-body {
  flex: 1;
  overflow-y: auto;
  padding: 16px;
}

.settings-label {
  font-size: 12px;
  font-weight: 600;
  color: var(--gray-text);
  text-transform: uppercase;
  letter-spacing: 0.6px;
  margin: 18px 0 8px;
}

#settings-store-name {
  width: 100%;
  padding: 12px 14px;
  font-size: 16px;
  border: 1px solid var(--gray-line);
  border-radius: var(--radius);
  outline: none;
  font-family: inherit;
}

#settings-store-name:focus { border-color: #888; }

.settings-toggle-row {
  display: flex;
  align-items: center;
  padding: 13px 0;
  border-bottom: 1px solid var(--gray-line);
}

.settings-toggle-row label {
  flex: 1;
  font-size: 15px;
}

/* iOS-style toggle */
.toggle-switch {
  position: relative;
  width: 50px;
  height: 30px;
  flex-shrink: 0;
}

.toggle-switch input { display: none; }

.toggle-slider {
  position: absolute;
  inset: 0;
  background: #ccc;
  border-radius: 30px;
  transition: background 0.2s;
}

.toggle-slider::before {
  content: '';
  position: absolute;
  width: 26px;
  height: 26px;
  left: 2px;
  top: 2px;
  background: var(--white);
  border-radius: 50%;
  box-shadow: 0 1px 4px rgba(0,0,0,0.25);
  transition: transform 0.2s;
}

.toggle-switch input:checked + .toggle-slider { background: var(--black); }
.toggle-switch input:checked + .toggle-slider::before { transform: translateX(20px); }

#settings-save-btn {
  width: 100%;
  margin-top: 24px;
  margin-bottom: 40px;
  padding: 15px;
  border: none;
  border-radius: var(--radius);
  background: var(--black);
  color: var(--white);
  font-size: 16px;
  font-weight: 600;
}


/* ===== CAMERA SCREEN ===== */
#camera-screen {
  position: fixed;
  inset: 0;
  background: #000;
  z-index: 400;
  display: flex;
  flex-direction: column;
}

#camera-header {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  z-index: 10;
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 16px;
  background: rgba(0,0,0,0.5);
  color: var(--white);
  font-size: 15px;
}

#camera-close {
  background: none;
  border: none;
  color: var(--white);
  font-size: 22px;
}

#camera-video {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

#camera-capture {
  position: absolute;
  bottom: 48px;
  left: 50%;
  transform: translateX(-50%);
  width: 72px;
  height: 72px;
  border-radius: 50%;
  background: var(--white);
  border: 5px solid rgba(255,255,255,0.5);
  box-shadow: 0 2px 12px rgba(0,0,0,0.3);
}

#camera-capture:active { transform: translateX(-50%) scale(0.93); }


/* ===== OCR PROCESSING ===== */
#ocr-processing {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.65);
  z-index: 500;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 16px;
  color: var(--white);
  font-size: 16px;
}

.spinner {
  width: 44px;
  height: 44px;
  border: 3px solid rgba(255,255,255,0.3);
  border-top-color: var(--white);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}

@keyframes spin { to { transform: rotate(360deg); } }


/* ===== OCR RESULTS SCREEN ===== */
#ocr-results-screen {
  position: fixed;
  inset: 0;
  background: var(--white);
  z-index: 300;
  display: flex;
  flex-direction: column;
}

#ocr-results-list {
  flex: 1;
  overflow-y: auto;
  padding: 12px 16px;
}

.ocr-result-row {
  display: flex;
  align-items: center;
  padding: 10px 0;
  border-bottom: 1px solid var(--gray-line);
  gap: 8px;
}

.ocr-result-row label {
  flex: 1;
  font-size: 15px;
}

.ocr-result-row input {
  width: 110px;
  padding: 8px 10px;
  text-align: right;
  font-size: 16px;
  border: 1px solid var(--gray-line);
  border-radius: 8px;
  outline: none;
  font-family: inherit;
}

.ocr-result-row input:focus { border-color: #888; }
.ocr-result-row .unit { font-size: 14px; color: #555; }

#ocr-apply-btn {
  flex-shrink: 0;
  margin: 12px 16px 0;
  margin-bottom: max(24px, env(safe-area-inset-bottom));
  padding: 15px;
  border: none;
  border-radius: var(--radius);
  background: var(--black);
  color: var(--white);
  font-size: 16px;
  font-weight: 600;
}


/* ===== CONFIRM DIALOG ===== */
#confirm-dialog {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.4);
  z-index: 600;
  display: flex;
  align-items: center;
  justify-content: center;
}

.dialog-box {
  background: var(--white);
  border-radius: 14px;
  padding: 24px 20px 18px;
  width: 285px;
  text-align: center;
  box-shadow: 0 4px 24px rgba(0,0,0,0.18);
}

.dialog-title {
  font-size: 17px;
  font-weight: 600;
  margin-bottom: 8px;
}

.dialog-msg {
  font-size: 14px;
  color: #555;
  margin-bottom: 20px;
  line-height: 1.5;
}

.dialog-buttons {
  display: flex;
  gap: 8px;
}

.dialog-buttons button {
  flex: 1;
  padding: 12px;
  border: none;
  border-radius: 8px;
  font-size: 16px;
}

#confirm-cancel {
  background: var(--gray-bg);
  color: var(--black);
}

#confirm-ok {
  background: var(--danger);
  color: var(--white);
  font-weight: 600;
}


/* ===== DATE PICKER ===== */
#date-picker {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.35);
  z-index: 600;
  display: flex;
  align-items: flex-end;
}

#date-picker-panel {
  background: var(--white);
  width: 100%;
  border-radius: 18px 18px 0 0;
  padding: 22px 18px 36px;
}

.date-picker-row {
  display: flex;
  gap: 8px;
  margin-bottom: 16px;
}

.date-picker-row select {
  flex: 1;
  padding: 10px 6px;
  font-size: 16px;
  border: 1px solid var(--gray-line);
  border-radius: 8px;
  background: var(--white);
  font-family: inherit;
  outline: none;
  -webkit-appearance: auto;
}

#dp-confirm {
  width: 100%;
  padding: 14px;
  border: none;
  border-radius: var(--radius);
  background: var(--black);
  color: var(--white);
  font-size: 16px;
  font-weight: 600;
}


/* ===== TOAST ===== */
#toast {
  position: fixed;
  bottom: 90px;
  left: 50%;
  transform: translateX(-50%);
  background: rgba(0,0,0,0.78);
  color: var(--white);
  padding: 9px 20px;
  border-radius: 20px;
  font-size: 14px;
  z-index: 900;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.25s;
  white-space: nowrap;
}

#toast.show { opacity: 1; }


/* ===== DRAG GHOST ===== */
#drag-ghost {
  position: fixed;
  background: rgba(0,0,0,0.82);
  color: var(--white);
  padding: 8px 16px;
  border-radius: 20px;
  font-size: 14px;
  z-index: 800;
  pointer-events: none;
  display: none;
  transform: translate(-50%, -50%);
}


/* ===== PIN LOCK SCREEN ===== */
#pin-screen {
  position: fixed;
  inset: 0;
  background: var(--white);
  z-index: 1000;
  display: flex;
  align-items: center;
  justify-content: center;
}

#pin-container {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0;
  width: 100%;
  max-width: 320px;
  padding: 0 24px;
}

#pin-app-title {
  font-size: 22px;
  font-weight: 700;
  margin-bottom: 32px;
  color: var(--black);
}

#pin-prompt {
  font-size: 15px;
  color: #555;
  margin-bottom: 24px;
}

#pin-dots, #pin-setup-dots {
  display: flex;
  gap: 18px;
  margin-bottom: 10px;
}

.pin-dot {
  width: 18px;
  height: 18px;
  border-radius: 50%;
  border: 2px solid #ccc;
  background: transparent;
  transition: background 0.15s, border-color 0.15s;
}

.pin-dot.filled {
  background: var(--black);
  border-color: var(--black);
}

#pin-error, #pin-setup-error {
  font-size: 13px;
  color: var(--danger);
  min-height: 20px;
  margin-bottom: 8px;
  text-align: center;
}

/* PIN keypad */
.pin-keypad {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 12px;
  width: 100%;
  margin-top: 16px;
}

.pin-key {
  padding: 20px;
  font-size: 26px;
  font-weight: 300;
  background: #f0f0f0;
  border: none;
  border-radius: 12px;
  color: var(--black);
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
}

.pin-key:active { background: #d8d8d8; }

.pin-key.pin-key-del {
  font-size: 20px;
  background: #e0e0e0;
}

.pin-key.pin-key-empty {
  background: transparent;
  pointer-events: none;
}

@keyframes shake {
  0%, 100% { transform: translateX(0); }
  20%       { transform: translateX(-8px); }
  40%       { transform: translateX(8px); }
  60%       { transform: translateX(-6px); }
  80%       { transform: translateX(6px); }
}

.shake { animation: shake 0.5s ease; }

/* ===== PIN SETUP SCREEN ===== */
#pin-setup-screen {
  position: fixed;
  inset: 0;
  background: var(--white);
  z-index: 1000;
  display: flex;
  flex-direction: column;
}

#pin-setup-body {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 40px 24px;
  max-width: 320px;
  margin: 0 auto;
  width: 100%;
}

#pin-setup-prompt {
  font-size: 15px;
  color: #555;
  margin-bottom: 24px;
  text-align: center;
}

/* ===== UTILITY ===== */
[hidden] { display: none !important; }