/* ============================================================
   Sampann Multigrain Atta Calculator — Stylesheet
   ============================================================ */

/* ---- Design Tokens ---- */
:root {
  color-scheme: light;

  --bg:           #ffffff;
  --bg-soft:      #faf8f4;
  --card:         #ffffff;
  --border:       #ece7dd;
  --text:         #241f18;
  --text-muted:   #8a8074;
  --accent:       #c8862f;
  --accent-dark:  #a56b1f;
  --accent-soft:  #f5e6cd;
  --good:         #3e8f5c;
  --bad:          #c94b4b;
  --radius:       18px;
  --shadow:       0 8px 24px rgba(60, 45, 20, 0.08), 0 2px 6px rgba(60, 45, 20, 0.05);

  /* Safe area insets for notched phones (iPhone X+, Android cutouts) */
  --sat: env(safe-area-inset-top,    0px);
  --sar: env(safe-area-inset-right,  0px);
  --sab: env(safe-area-inset-bottom, 0px);
  --sal: env(safe-area-inset-left,   0px);

  /* Topbar height — referenced by total-card sticky offset */
  --topbar-h: 68px;
}

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

html {
  scroll-padding-top: calc(var(--topbar-h) + var(--sat));
  height: 100%;
  background: var(--bg);
  /* Prevent font scaling in landscape on iOS */
  -webkit-text-size-adjust: 100%;
      text-size-adjust: 100%;
}

body {
  min-height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: 'Poppins', system-ui, -apple-system, Segoe UI, Roboto, sans-serif;
  -webkit-font-smoothing: antialiased;
  /* Respect safe-area on the sides (landscape notch) */
  padding-left:  var(--sal);
  padding-right: var(--sar);
}

::selection { background: var(--accent-soft); }

/* ---- Utilities ---- */
.hidden { display: none !important; }

/* Hide number-input spinners (ugly on mobile) */
input[type="number"]::-webkit-inner-spin-button,
input[type="number"]::-webkit-outer-spin-button { -webkit-appearance: none; margin: 0; }
input[type="number"] { -moz-appearance: textfield; appearance: textfield; }

/* ============================================================
   Layout
   ============================================================ */
.app-shell {
  max-width: 760px;
  margin: 0 auto;
  /* bottom padding = sticky-bar height + safe-area-bottom */
  padding: 0 12px calc(80px + var(--sab));
}

/* ============================================================
   Top Bar
   ============================================================ */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  /* Top padding accounts for status-bar safe area (notched phones) */
  padding: calc(10px + var(--sat)) 12px 10px;
  position: sticky;
  top: 0;
  background: var(--bg);
  z-index: 20;
  border-bottom: 1px solid var(--border);
  min-height: var(--topbar-h);
}

.brand {
  display: flex;
  align-items: center;
  gap: 10px;
  min-width: 0;
  flex: 1;
}

.logo {
  width: 44px;
  height: 44px;
  border-radius: 50%;
  flex-shrink: 0;
  background: #fff;
  border: 1.5px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: var(--shadow);
  overflow: hidden;
}

.logo img { width: 100%; height: 100%; object-fit: cover; }
.logo svg { width: 26px; height: 26px; }

.brand-text { min-width: 0; flex: 1; }

.store-name {
  font-weight: 700;
  font-size: 15px;
  line-height: 1.2;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  /* Fluid max-width so it never overlaps the settings button */
  max-width: calc(100vw - 130px);
}

.tagline {
  font-size: 11px;
  color: var(--text-muted);
  margin-top: 1px;
}

.sync-badge {
  font-size: 10px;
  color: var(--text-muted);
  margin-top: 2px;
  display: flex;
  align-items: center;
  gap: 4px;
}

.sync-dot {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: var(--good);
  flex-shrink: 0;
  display: inline-block;
}

.sync-dot.off { background: var(--bad); }

/* Settings button — 44px touch target (Apple HIG / Material min) */
.icon-btn {
  width: 44px;
  height: 44px;
  border-radius: 50%;
  border: 1px solid var(--border);
  background: var(--bg-soft);
  font-size: 20px;
  cursor: pointer;
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  /* Slightly larger tap surface without visual change */
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
}

.icon-btn:active { transform: scale(0.92); }

/* ============================================================
   Main / Calculator View
   ============================================================ */
main { padding-top: 16px; }

/* ---- Total Card ---- */
.total-card {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  background: var(--bg-soft);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px 16px;
  margin-bottom: 14px;
  position: sticky;
  /* Sit just below the topbar */
  top: var(--topbar-h);
  z-index: 10;
}

.total-card .label { font-size: 13px; color: var(--text-muted); font-weight: 500; }
.total-card .value { font-size: 26px; font-weight: 800; letter-spacing: -0.5px; flex-shrink: 0; }
.total-card .value.good { color: var(--good); }
.total-card .value.bad  { color: var(--accent-dark); }
.total-card .hint  { font-size: 11px; color: var(--text-muted); margin-top: 2px; }

/* ---- Grain Grid ---- */
/* Mobile-first: 2 columns on narrow phones */
.grain-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 10px;
}

.grain-card {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 14px 10px 12px;
  box-shadow: var(--shadow);
  position: relative;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
  transition: border-color 0.15s ease, box-shadow 0.15s ease;
  /* Prevent text selection on rapid taps */
  -webkit-user-select: none;
      user-select: none;
}

.grain-card.active {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-soft), var(--shadow);
}

.grain-icon {
  width: 42px;
  height: 42px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 20px;
}

.grain-name {
  font-size: 12px;
  font-weight: 600;
  text-align: center;
  line-height: 1.3;
  min-height: 32px;
  display: flex;
  align-items: center;
}

/* ---- Stepper ---- */
.stepper {
  display: flex;
  align-items: center;
  gap: 4px;
  width: 100%;
}

/* 44px touch target height for step buttons */
.step-btn {
  width: 36px;
  height: 44px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--bg-soft);
  font-size: 18px;
  font-weight: 700;
  color: var(--accent-dark);
  cursor: pointer;
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  line-height: 1;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
}

.step-btn:active { transform: scale(0.90); }

/* 16px font-size prevents iOS auto-zoom on input focus */
.percent-input {
  width: 100%;
  text-align: center;
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 6px 2px;
  font-family: inherit;
  font-weight: 700;
  font-size: 16px;
  color: var(--text);
  background: #fff;
  -webkit-tap-highlight-color: transparent;
}

.percent-input:focus {
  outline: 2px solid var(--accent-soft);
  border-color: var(--accent);
}

.percent-suffix {
  font-size: 10px;
  color: var(--text-muted);
  margin-top: -4px;
}

/* ---- Remove button (custom grains) ---- */
.remove-btn {
  position: absolute;
  top: 6px;
  right: 6px;
  /* 28px visible, but 44px touch zone via padding trick */
  width: 28px;
  height: 28px;
  border-radius: 50%;
  border: none;
  background: var(--bad);
  color: #fff;
  font-size: 12px;
  line-height: 1;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  -webkit-tap-highlight-color: transparent;
}

/* ---- Add Item Button ---- */
.add-item-btn {
  width: 100%;
  margin-top: 14px;
  padding: 16px;
  border-radius: 14px;
  border: 1.5px dashed var(--border);
  background: transparent;
  color: var(--text-muted);
  font-family: inherit;
  font-weight: 600;
  font-size: 14px;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
}

.add-item-btn:active { background: var(--bg-soft); }

/* ============================================================
   Sticky Action Bar
   ============================================================ */
.sticky-actions {
  position: fixed;
  /* Span full width then centre with max-width */
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 30;
  background: var(--bg);
  border-top: 1px solid var(--border);
  /* Ensure content is centred like the app-shell */
  display: flex;
  justify-content: center;
}

/* Inner wrapper mirrors app-shell width & padding */
.sticky-actions-inner {
  display: flex;
  gap: 10px;
  width: 100%;
  max-width: 760px;
  padding: 10px 12px calc(10px + var(--sab));
}

/* ---- Button Styles ---- */
.primary-btn,
.secondary-btn,
.ghost-btn {
  font-family: inherit;
  font-weight: 700;
  font-size: 15px;
  border-radius: 14px;
  /* Minimum 48px height for accessibility */
  padding: 13px 16px;
  min-height: 48px;
  cursor: pointer;
  border: none;
  flex: 1;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
}

.primary-btn {
  background: var(--accent);
  color: #fff;
  box-shadow: 0 6px 16px rgba(200, 134, 47, 0.35);
}

.primary-btn:disabled {
  background: #e3ddd0;
  color: #a89f8f;
  box-shadow: none;
  cursor: not-allowed;
}

.primary-btn:active:not(:disabled) { transform: scale(0.98); }

.secondary-btn {
  background: var(--bg-soft);
  color: var(--text);
  border: 1px solid var(--border);
}

.secondary-btn:active { transform: scale(0.98); }

/* ============================================================
   Bill / Receipt View
   ============================================================ */
.receipt-wrap {
  display: flex;
  justify-content: center;
  padding: 6px 0 16px;
}

.receipt {
  width: 100%;
  max-width: 340px;
  background: #fff;
  border: 1px solid var(--border);
  border-radius: 16px;
  box-shadow: var(--shadow);
  padding: 22px 20px;
  font-family: 'Poppins', monospace;
}

.r-logo             { display: flex; justify-content: center; margin-bottom: 8px; }
.r-logo img,
.r-logo svg         { width: 52px; height: 52px; border-radius: 50%; object-fit: cover; }
.r-store            { text-align: center; font-weight: 800; font-size: 17px; }
.r-tagline          { text-align: center; font-size: 11px; color: var(--text-muted); margin-top: 2px; }
.r-divider          { border: none; border-top: 1px solid var(--border); margin: 12px 0; }
.r-divider.dashed   { border-top: 1px dashed #c9c2b4; }
.r-row              { display: flex; justify-content: space-between; font-size: 13px; padding: 2px 0; color: var(--text-muted); }
.r-token            { font-weight: 800; color: var(--accent-dark); font-size: 15px; }
.r-items            { display: flex; flex-direction: column; gap: 5px; margin: 4px 0; }
.r-item             { display: flex; justify-content: space-between; font-size: 13.5px; }
.r-item span:last-child { font-weight: 700; }
.r-total            { font-size: 15px; font-weight: 800; color: var(--text); }
.r-footer           { text-align: center; font-size: 12px; color: var(--text-muted); margin-top: 14px; }

.bill-actions { display: flex; gap: 10px; padding: 0 0 16px; }

/* ============================================================
   Settings Modal
   ============================================================ */
.modal {
  position: fixed;
  inset: 0;
  background: rgba(30, 24, 14, 0.50);
  z-index: 50;
  display: flex;
  align-items: flex-end;
  justify-content: center;
  /* Smooth backdrop */
  backdrop-filter: blur(2px);
  -webkit-backdrop-filter: blur(2px);
  padding: 0 var(--sal) 0 var(--sar);
}

.modal-card {
  background: #fff;
  width: 100%;
  max-width: 480px;
  border-radius: 20px 20px 0 0;
  padding: 22px 20px;
  /* Safe area bottom so content isn't behind home bar */
  padding-bottom: calc(22px + var(--sab));
  max-height: 92vh;
  overflow-y: auto;
  /* Smooth scroll on iOS */
  -webkit-overflow-scrolling: touch;
}

.modal-card h3 { margin: 0 0 16px; font-size: 18px; }

.field          { margin-bottom: 16px; }
.field label    { display: block; font-size: 12.5px; font-weight: 600; color: var(--text-muted); margin-bottom: 6px; }

/* 16px prevents iOS zoom on focus */
.field input[type="text"] {
  width: 100%;
  padding: 13px 12px;
  border-radius: 10px;
  border: 1px solid var(--border);
  font-family: inherit;
  font-size: 16px;
}

.logo-row         { display: flex; align-items: center; gap: 12px; flex-wrap: wrap; }
.logo-row .logo   { width: 52px; height: 52px; }

.file-btn {
  padding: 11px 16px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--bg-soft);
  font-family: inherit;
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
  min-height: 44px;
  -webkit-tap-highlight-color: transparent;
}

.new-item-row { display: flex; gap: 8px; flex-wrap: wrap; }
.new-item-row input { flex: 1; min-width: 120px; }
.new-item-row input.emoji-field { flex: 0 0 54px; text-align: center; min-width: unset; }

.modal-actions { display: flex; gap: 10px; margin-top: 8px; }

.item-manage-list {
  display: flex;
  flex-direction: column;
  gap: 6px;
  max-height: 160px;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  margin-bottom: 10px;
}

.item-manage-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  font-size: 13px;
  padding: 8px 10px;
  background: var(--bg-soft);
  border-radius: 8px;
  min-height: 40px;
}

.item-manage-row button {
  border: none;
  background: none;
  color: var(--bad);
  font-size: 13px;
  cursor: pointer;
  font-weight: 700;
  padding: 4px 8px;
  min-height: 36px;
  -webkit-tap-highlight-color: transparent;
}

/* ============================================================
   Toast Notification
   ============================================================ */
.toast {
  position: fixed;
  left: 50%;
  transform: translateX(-50%);
  /* Float above sticky bar + safe area */
  bottom: calc(76px + var(--sab));
  z-index: 60;
  background: #2a241a;
  color: #fff;
  padding: 10px 18px;
  border-radius: 24px;
  font-size: 13px;
  font-weight: 500;
  box-shadow: 0 6px 18px rgba(0, 0, 0, 0.28);
  white-space: nowrap;
  max-width: calc(100vw - 32px);
  text-align: center;
}

/* ============================================================
   Print Styles
   ============================================================ */
@media print {
  body * { visibility: hidden; }

  #bill,
  #bill * { visibility: visible; }

  #bill {
    position: absolute;
    top: 0;
    left: 0;
    width: 80mm;
    max-width: 80mm;
    padding: 6mm;
    box-shadow: none;
    border: none;
    border-radius: 0;
  }

  .no-print { display: none !important; }
}

/* ============================================================
   Responsive Breakpoints
   ============================================================ */

/* ── Small phones (≤ 360px) ─────────────────────────────── */
@media (max-width: 360px) {
  .store-name    { font-size: 13px; }
  .tagline       { display: none; } /* hide to save space */
  .total-card .value { font-size: 22px; }
  .grain-name    { font-size: 11px; }
  .step-btn      { width: 32px; font-size: 16px; }
  .percent-input { font-size: 15px; }
}

/* ── Landscape phone (height ≤ 430px) ────────────────────── */
@media (max-height: 430px) and (orientation: landscape) {
  :root { --topbar-h: 52px; }

  .topbar { padding-top: calc(6px + var(--sat)); padding-bottom: 6px; }
  .sync-badge { display: none; }          /* trim topbar height */
  .total-card { padding: 10px 14px; margin-bottom: 10px; }
  .total-card .value { font-size: 20px; }

  /* 3 columns in landscape */
  .grain-grid { grid-template-columns: repeat(3, 1fr); gap: 8px; }
  .grain-icon { width: 34px; height: 34px; font-size: 17px; }
  .grain-name { min-height: 24px; font-size: 11px; }
  .step-btn   { height: 38px; }

  /* Compact sticky bar */
  .sticky-actions-inner { padding-top: 8px; padding-bottom: calc(8px + var(--sab)); }
  .primary-btn,
  .secondary-btn { min-height: 40px; padding: 9px 12px; font-size: 14px; }
}

/* ── Tablet portrait / large phone (≥ 480px) ─────────────── */
@media (min-width: 480px) {
  .grain-grid { grid-template-columns: repeat(3, 1fr); gap: 12px; }
  .store-name { font-size: 16px; max-width: calc(100vw - 160px); }
  .logo       { width: 48px; height: 48px; }
}

/* ── Tablet landscape / small laptop (≥ 640px) ───────────── */
@media (min-width: 640px) {
  .grain-grid { grid-template-columns: repeat(3, 1fr); gap: 14px; }

  .modal { align-items: center; }       /* centre modal vertically */
  .modal-card {
    border-radius: 20px;
    max-height: 80vh;
    padding-bottom: 22px;               /* no safe-area needed on desktop */
  }
}

/* ── Desktop / laptop (≥ 768px) ──────────────────────────── */
@media (min-width: 768px) {
  :root { --topbar-h: 72px; }

  .app-shell { padding-left: 24px; padding-right: 24px; }

  .logo       { width: 52px; height: 52px; }
  .store-name { font-size: 17px; }
  .tagline    { font-size: 12px; }

  .grain-grid { grid-template-columns: repeat(4, 1fr); gap: 16px; }
  .grain-card { padding: 16px 14px 14px; }
  .grain-name { font-size: 13px; }
  .step-btn   { width: 36px; height: 44px; }

  .total-card .value { font-size: 28px; }

  .sticky-actions-inner { padding-left: 24px; padding-right: 24px; }

  /* On desktop the sticky bar should be slightly narrower */
  .receipt { max-width: 380px; }
}

/* ── Wide desktop (≥ 1024px) ─────────────────────────────── */
@media (min-width: 1024px) {
  .grain-grid { grid-template-columns: repeat(4, 1fr); gap: 18px; }

  /* Hover states (pointer devices only) */
  .grain-card:hover       { border-color: var(--accent-soft); }
  .grain-card.active:hover{ border-color: var(--accent); }
  .step-btn:hover         { background: var(--accent-soft); border-color: var(--accent); }
  .add-item-btn:hover     { background: var(--bg-soft); border-color: var(--accent-soft); }
  .icon-btn:hover         { background: var(--accent-soft); }
  .primary-btn:hover:not(:disabled) { background: var(--accent-dark); }
  .secondary-btn:hover    { background: var(--border); }
}
