/* ============== Reset / Base ============== */
* { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --bg: #f7f8fa;
  --card: #ffffff;
  --text: #1a1a2e;
  --muted: #999;
  --border: #e8e8e8;
  --soft: #fafafa;
  --accent: #3b82f6;
  --accent-dim: #dbeafe;
  --green: #16a34a;
  --green-dim: #dcfce7;
  --red: #ef4444;
  --red-dim: #fee2e2;
  --amber: #f59e0b;
  --amber-dim: #fffbe6;
  --shadow: 0 1px 6px rgba(0,0,0,0.07);
  --shadow-lg: 0 6px 24px rgba(0,0,0,0.12);
  --radius: 12px;
  --radius-sm: 8px;
  --tap: 44px;
  --safe-bottom: env(safe-area-inset-bottom, 0);
  --safe-top: env(safe-area-inset-top, 0);
}

html, body {
  min-height: 100vh;
  background: var(--bg);
  color: var(--text);
  font-family: 'Hiragino Kaku Gothic ProN', 'Noto Sans JP', system-ui, -apple-system, sans-serif;
  font-size: 14px;
  line-height: 1.5;
  -webkit-text-size-adjust: 100%;
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior: none;
}
body { overflow: hidden; }

button, input, textarea, select {
  font: inherit;
  color: inherit;
}
button { cursor: pointer; border: none; background: none; }

input, textarea, select {
  border: 1.5px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 10px 12px;
  width: 100%;
  background: var(--soft);
  outline: none;
  transition: border-color 0.15s;
}
input:focus, textarea:focus, select:focus { border-color: var(--accent); }

/* ============== Tab scroll container ============== */
.tab-scroll {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  display: flex;
  overflow-x: auto;
  overflow-y: hidden;
  scroll-snap-type: x mandatory;
  -webkit-overflow-scrolling: touch;
  scrollbar-width: none;
}
.tab-scroll::-webkit-scrollbar { display: none; }

.tab {
  flex: 0 0 100%;
  width: 100%;
  height: 100%;
  overflow-y: auto;
  scroll-snap-align: start;
  scroll-snap-stop: always;
  padding: calc(12px + var(--safe-top)) 16px calc(12px + var(--safe-bottom));
  -webkit-overflow-scrolling: touch;
}

/* ============== Tab bar (in-content, sticky on list tabs) ============== */
.tabs-section {
  display: flex;
  overflow-x: auto;
  overflow-y: hidden;
  scrollbar-width: none;
  -webkit-overflow-scrolling: touch;
  background: var(--card);
  border-radius: var(--radius-sm);
  padding: 4px;
  gap: 4px;
}
.tabs-section::-webkit-scrollbar { display: none; }

.sticky-tabs {
  position: sticky;
  top: -1px;
  z-index: 10;
  margin: 0 -4px 8px;
  padding: 6px 4px;
  border-radius: 0;
  border-bottom: 1px solid var(--border);
  box-shadow: 0 2px 8px rgba(0,0,0,0.04);
  background: var(--bg);
}

.tab-chip {
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 8px 12px;
  border-radius: var(--radius-sm);
  font-size: 13px;
  color: var(--muted);
  white-space: nowrap;
  max-width: 180px;
  background: var(--soft);
  border: 1.5px solid transparent;
  min-height: 36px;
}
.tab-chip.active {
  color: var(--accent);
  font-weight: 800;
  background: var(--accent-dim);
  border-color: var(--accent);
}
.tab-chip .tab-name {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  max-width: 110px;
}
.tab-chip .tab-close {
  width: 22px;
  height: 22px;
  border-radius: 50%;
  display: grid;
  place-items: center;
  font-size: 12px;
  color: var(--muted);
}
.tab-chip .tab-close:active { background: var(--red-dim); color: var(--red); }
.tab-chip .tab-shared {
  font-size: 11px;
  color: var(--green);
}

.tab-chip-plus {
  color: var(--accent);
  font-weight: 800;
  border-style: dashed;
  border-color: var(--accent);
}
.tab-chip-plus:active {
  background: var(--accent-dim);
}

.tabs-card { padding: 10px; }

/* ============== Home tab ============== */
.tab-home .home-title {
  font-size: 22px;
  font-weight: 800;
  text-align: center;
  margin: 4px 0 14px;
}

.quick-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
}
.quick-btn {
  background: var(--soft);
  border: 1.5px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 14px 10px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
  font-weight: 700;
  font-size: 13px;
  min-height: 72px;
  transition: all 0.15s;
}
.quick-btn .ic { font-size: 24px; }
.quick-btn:active { background: var(--accent-dim); border-color: var(--accent); transform: scale(0.98); }

.card-sep {
  border: none;
  border-top: 1px solid var(--border);
  margin: 14px 0 10px;
}
.card-sublabel {
  font-size: 12px;
  color: var(--muted);
  margin-bottom: 6px;
  font-weight: 600;
}

.home-bulk-input { resize: vertical; }
.home-bulk-row {
  display: flex;
  gap: 8px;
  margin-top: 8px;
  align-items: stretch;
}
.home-bulk-mode {
  width: 130px;
  flex: 0 0 auto;
}

.preset-list, .history-list {
  display: flex;
  flex-direction: column;
  gap: 6px;
}
.preset-row, .history-row {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px;
  background: var(--soft);
  border-radius: var(--radius-sm);
}
.preset-row .name, .history-row .name {
  flex: 1;
  font-size: 14px;
  font-weight: 700;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.preset-row .meta, .history-row .meta {
  font-size: 11px;
  color: var(--muted);
}
.preset-row .small-btn, .history-row .small-btn {
  padding: 6px 10px;
  background: var(--accent);
  color: #fff;
  border-radius: 6px;
  font-size: 12px;
  font-weight: 700;
  flex-shrink: 0;
}
.preset-row .small-btn.danger, .history-row .small-btn.danger {
  background: none;
  color: var(--red);
  border: 1px solid var(--red-dim);
}

.settings-row {
  display: flex;
  gap: 8px;
}
.card-hint {
  font-size: 11px;
  color: var(--muted);
  margin-top: 8px;
  line-height: 1.4;
}

/* ============== List tab header ============== */
.tab-list .header {
  display: flex;
  align-items: center;
  gap: 4px;
  margin-bottom: 6px;
}
.tab-list .icon-btn {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  display: grid;
  place-items: center;
  font-size: 18px;
  color: var(--muted);
  flex-shrink: 0;
}
.tab-list .icon-btn:active { background: var(--border); }
.tab-list .title {
  flex: 1;
  font-size: 16px;
  font-weight: 800;
  text-align: center;
  background: transparent;
  border: none;
  padding: 6px;
  min-width: 0;
}
.tab-list .title:focus {
  background: var(--soft);
  border-radius: var(--radius-sm);
  outline: 1.5px solid var(--accent);
}

.tab-list .meta-row {
  display: flex;
  justify-content: center;
  align-items: center;
  gap: 8px;
  margin-bottom: 10px;
}

/* Mode badge */
.mode-badge {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  font-size: 11px;
  font-weight: 700;
  letter-spacing: 1px;
  color: var(--muted);
  padding: 4px 10px;
  background: var(--card);
  border-radius: 99px;
  box-shadow: var(--shadow);
}
.mode-badge.shopping { color: var(--accent); }
.mode-badge.travel { color: var(--green); }
.mode-badge.other { color: #a855f7; }

/* ============== Card ============== */
.card {
  background: var(--card);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 14px;
  margin-bottom: 10px;
}
.card-label {
  font-size: 11px;
  font-weight: 700;
  color: var(--muted);
  letter-spacing: 2px;
  text-transform: uppercase;
  margin-bottom: 10px;
}

/* ============== Item list ============== */
.items-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 4px;
}

.items, .items-done {
  display: flex;
  flex-direction: column;
}
.empty {
  color: #ccc;
  font-size: 13px;
  text-align: center;
  padding: 24px 0;
}

.done-collapse {
  margin-top: 6px;
  border-top: 1px solid var(--border);
  padding-top: 6px;
}
.done-collapse summary {
  font-size: 12px;
  font-weight: 700;
  color: var(--muted);
  padding: 8px 4px;
  cursor: pointer;
  list-style: none;
}
.done-collapse summary::-webkit-details-marker { display: none; }
.done-collapse[open] summary { color: var(--text); }

.item-row {
  border-bottom: 1px solid #f3f4f6;
  background: var(--card);
}
.item-row:last-child { border-bottom: none; }

.item {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 4px;
  min-height: var(--tap);
  user-select: none;
}
.item.dragging { opacity: 0.4; }
.item-row.drop-target { border-top: 2px solid var(--accent); }

.item .check {
  width: 24px;
  height: 24px;
  border: 2px solid #d1d5db;
  border-radius: 6px;
  display: grid;
  place-items: center;
  flex-shrink: 0;
}
.item.done .check {
  background: var(--green);
  border-color: var(--green);
  color: #fff;
}
.item.done .check::after {
  content: '✓';
  font-size: 16px;
  font-weight: 800;
}

.item .text {
  flex: 1;
  font-size: 15px;
  word-break: break-word;
}
.item.done .text {
  text-decoration: line-through;
  color: var(--muted);
}

.item .price {
  font-size: 12px;
  color: var(--muted);
  margin-left: 4px;
}

.item .qty {
  font-size: 13px;
  font-weight: 700;
  color: var(--muted);
  background: var(--soft);
  padding: 4px 10px;
  border-radius: 99px;
  min-width: 36px;
  text-align: center;
}

.item .handle {
  width: 22px;
  text-align: center;
  color: #d1d5db;
  font-size: 16px;
  cursor: grab;
  flex-shrink: 0;
}

.item-delete {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  display: grid;
  place-items: center;
  color: #ccc;
  font-size: 16px;
  flex-shrink: 0;
}
.item-delete:active {
  background: var(--red-dim);
  color: var(--red);
}

/* ============== Add input ============== */
.add-row {
  display: flex;
  gap: 8px;
  margin-top: 8px;
}
.add-row input { flex: 1; margin: 0; }

.btn {
  padding: 10px 16px;
  border-radius: var(--radius-sm);
  font-weight: 700;
  font-size: 14px;
  min-height: var(--tap);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 4px;
  transition: opacity 0.15s, transform 0.05s;
  white-space: nowrap;
}
.btn:active { transform: scale(0.98); }
.btn:disabled { opacity: 0.4; pointer-events: none; }
.btn-primary { background: var(--accent); color: #fff; }
.btn-secondary { background: var(--soft); color: var(--text); border: 1.5px solid var(--border); }
.btn-ghost { color: var(--muted); }
.btn-danger { background: var(--red); color: #fff; }
.btn-block { width: 100%; }

/* ============== Split bill ============== */
.split-bill .row {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-bottom: 8px;
}
.split-bill .row label {
  font-size: 13px;
  width: 80px;
  color: var(--muted);
  flex-shrink: 0;
}
.split-bill .row input { margin: 0; }
.split-bill .result {
  margin-top: 10px;
  padding: 12px;
  background: var(--accent-dim);
  border-radius: var(--radius-sm);
  font-size: 16px;
  font-weight: 700;
  color: var(--accent);
  text-align: center;
}
.split-bill .result .small {
  font-size: 11px;
  font-weight: 500;
  color: var(--muted);
  display: block;
  margin-top: 2px;
}

/* ============== Export buttons (single line) ============== */
.export-row {
  display: flex;
  flex-wrap: nowrap;
  gap: 6px;
}
.export-row .export-btn {
  flex: 1 1 0;
  min-width: 0;
  font-size: 12px;
  padding: 10px 4px;
  white-space: nowrap;
  overflow: hidden;
}

/* ============== Modal ============== */
.modal-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.4);
  z-index: 200;
  display: none;
  align-items: flex-end;
  justify-content: center;
}
.modal-backdrop.open { display: flex; animation: fadein 0.15s; }
.modal {
  background: var(--card);
  border-radius: 16px 16px 0 0;
  padding: 20px;
  padding-bottom: calc(20px + var(--safe-bottom));
  width: 100%;
  max-width: 560px;
  max-height: 90vh;
  overflow-y: auto;
  animation: slideup 0.2s ease-out;
}
.modal-title {
  font-size: 16px;
  font-weight: 800;
  margin-bottom: 14px;
  text-align: center;
}
.set-section { margin-bottom: 14px; }

.check-row {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 4px;
  font-size: 14px;
  cursor: pointer;
}
.check-row input { width: auto; }

@keyframes fadein { from { opacity: 0; } to { opacity: 1; } }
@keyframes slideup { from { transform: translateY(100%); } to { transform: translateY(0); } }

/* ============== AI menu items ============== */
.ai-menu-item {
  display: grid;
  grid-template-columns: 30px 1fr;
  grid-template-rows: auto auto;
  align-items: center;
  gap: 2px 10px;
  width: 100%;
  padding: 12px;
  background: var(--soft);
  border: 1.5px solid var(--border);
  border-radius: var(--radius-sm);
  margin-bottom: 8px;
  text-align: left;
}
.ai-menu-item:active {
  background: var(--accent-dim);
  border-color: var(--accent);
}
.ai-menu-item .ic {
  grid-row: 1 / span 2;
  font-size: 22px;
  text-align: center;
}
.ai-menu-item .lbl {
  font-weight: 700;
  font-size: 14px;
}
.ai-menu-item .hint {
  font-size: 11px;
  color: var(--muted);
}

/* ============== Qty wheel ============== */
.wheel-container { position: relative; margin-bottom: 12px; }
.wheel {
  height: 200px;
  overflow-y: scroll;
  scroll-snap-type: y mandatory;
  border-radius: var(--radius-sm);
  background: var(--soft);
  -webkit-overflow-scrolling: touch;
}
.wheel-pad { height: 80px; }
.wheel-item {
  height: 40px;
  scroll-snap-align: center;
  display: grid;
  place-items: center;
  font-size: 18px;
  color: var(--muted);
  transition: color 0.15s;
}
.wheel-item.active {
  color: var(--accent);
  font-weight: 800;
  font-size: 24px;
}
.wheel-overlay {
  pointer-events: none;
  position: absolute;
  left: 0;
  right: 0;
  top: 50%;
  transform: translateY(-50%);
  height: 40px;
  border-top: 1.5px solid var(--accent);
  border-bottom: 1.5px solid var(--accent);
}

/* ============== Drawer ============== */
.drawer {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.4);
  z-index: 250;
  display: none;
}
.drawer.open { display: block; }
.drawer-panel {
  position: absolute;
  right: 0;
  top: 0;
  bottom: 0;
  width: 100%;
  max-width: 480px;
  background: var(--card);
  display: flex;
  flex-direction: column;
  animation: slidein 0.2s ease-out;
  padding-bottom: var(--safe-bottom);
}
@keyframes slidein { from { transform: translateX(100%); } to { transform: translateX(0); } }
.drawer-header {
  padding: 12px 16px;
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  gap: 8px;
}
.drawer-title { flex: 1; font-weight: 800; }
.drawer-body {
  flex: 1;
  overflow-y: auto;
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 12px;
}
.bubble {
  padding: 10px 14px;
  border-radius: 14px;
  max-width: 85%;
  font-size: 14px;
  line-height: 1.5;
  white-space: pre-wrap;
  word-break: break-word;
}
.bubble.user { background: var(--accent); color: #fff; align-self: flex-end; }
.bubble.ai { background: var(--soft); color: var(--text); align-self: flex-start; }
.drawer-foot {
  padding: 10px 12px;
  border-top: 1px solid var(--border);
  display: flex;
  gap: 8px;
}
.drawer-foot input { flex: 1; margin: 0; }
.drawer-confirm {
  padding: 10px 12px;
  border-top: 1px solid var(--border);
}
.build-draft {
  border-bottom: 1px solid var(--border);
  background: var(--accent-dim);
  padding: 12px 16px;
  max-height: 200px;
  overflow-y: auto;
}
.build-draft-head {
  display: flex;
  justify-content: space-between;
  margin-bottom: 6px;
}
.build-draft-name { font-weight: 800; font-size: 13px; }
.build-draft-count { font-size: 11px; color: var(--muted); }
.build-draft-items {
  list-style: none;
  font-size: 13px;
}
.build-draft-items li {
  padding: 3px 0;
  display: flex;
  justify-content: space-between;
}

/* ============== Toast ============== */
.toast-stack {
  position: fixed;
  top: calc(var(--safe-top) + 12px);
  left: 50%;
  transform: translateX(-50%);
  z-index: 300;
  display: flex;
  flex-direction: column;
  gap: 6px;
  pointer-events: none;
  width: calc(100% - 32px);
  max-width: 480px;
}
.toast {
  background: rgba(26, 26, 46, 0.92);
  color: #fff;
  padding: 10px 14px;
  border-radius: var(--radius-sm);
  font-size: 13px;
  text-align: center;
  box-shadow: var(--shadow-lg);
  animation: toast-in 0.2s, toast-out 0.2s 2.6s forwards;
  pointer-events: auto;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 12px;
}
.toast.success { background: rgba(22, 163, 74, 0.92); }
.toast.error { background: rgba(239, 68, 68, 0.92); }
.toast.warn { background: rgba(245, 158, 11, 0.92); }
.toast .toast-action {
  background: rgba(255,255,255,0.18);
  color: #fff;
  padding: 4px 10px;
  border-radius: 6px;
  font-weight: 700;
  font-size: 12px;
}
@keyframes toast-in { from { opacity: 0; transform: translateY(-8px); } to { opacity: 1; transform: translateY(0); } }
@keyframes toast-out { to { opacity: 0; transform: translateY(-8px); } }

/* ============== Sync indicator ============== */
.sync-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: #d1d5db;
  flex-shrink: 0;
  transition: background 0.2s;
}
.sync-dot.connected { background: var(--green); }
.sync-dot.syncing { background: var(--amber); animation: pulse 1.2s infinite; }
.sync-dot.error { background: var(--red); }
.sync-dot.local { background: #d1d5db; }
@keyframes pulse { 50% { opacity: 0.4; } }

/* ============== OCR suggestion list ============== */
.suggest-list {
  display: flex;
  flex-direction: column;
  gap: 6px;
  margin-top: 10px;
}
.suggest-row {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px;
  background: var(--soft);
  border-radius: var(--radius-sm);
}
.suggest-row .name { flex: 1; font-size: 14px; }
.suggest-row .matched { color: var(--green); font-size: 11px; font-weight: 700; }
.suggest-row .add-btn {
  padding: 6px 12px;
  background: var(--accent);
  color: #fff;
  border-radius: 6px;
  font-size: 12px;
  font-weight: 700;
}

.hidden { display: none !important; }

@media (min-width: 600px) {
  .quick-grid { grid-template-columns: 1fr 1fr 1fr 1fr; }
}
