/* Reset */
* { box-sizing: border-box; margin: 0; padding: 0; }
html, body { height: 100%; }
body { font-family: system-ui, -apple-system, Segoe UI, Roboto, Arial, sans-serif; background: #e5e7eb; color: #1f2937; }
button { cursor: pointer; }
.hidden { display: none !important; }
/* Disable zoom */
html { touch-action: manipulation; }
body { user-select: none; -webkit-user-select: none; -webkit-touch-callout: none; }

/* Layout */
#top-bar { --control-h: 36px; position: sticky; top: 0; z-index: 10; display: flex; align-items: center; justify-content: space-between; gap: 8px; padding: 10px 12px; background: #ffffff; border-bottom: 1px solid #e5e7eb; box-shadow: 0 1px 0 rgba(0,0,0,0.04); }
.app-logo { height: 32px; width: auto; margin-right: 8px; }
#top-bar .left, #top-bar .center, #top-bar .right { display: flex; align-items: center; gap: 8px; }
#top-bar .center { flex: 1 1 auto; min-width: 0; justify-content: center; }
#top-bar #add-btn { width: var(--control-h); height: var(--control-h); border-radius: 8px; border: 1px solid #2563eb; background: #2563eb; color: #fff; font-size: 18px; font-weight: 700; line-height: 0; display: grid; place-items: center; }
#top-bar #import-btn, #top-bar #export-btn { padding: 6px 10px; border-radius: 8px; border: 1px solid #d1d5db; background: #ffffff; }
#top-bar .btn-save { height: var(--control-h); padding: 0 12px; border-radius: 8px; border: 1px solid #7c3aed; background: #7c3aed; color: #fff; font-weight: 700; display: inline-flex; align-items: center; }
#top-bar .btn-save:hover { background: #6d28d9; border-color: #6d28d9; }
#top-bar #save-btn { background: #5b21b6; border-color: #5b21b6; color: #fff; font-weight: 700; }
#top-bar #actions-btn { background: #8b5cf6; border-color: #8b5cf6; color: #fff; font-weight: 700; }
#top-bar #settings-btn { height: var(--control-h); padding: 0 10px; border-radius: 8px; border: 1px solid #fdba74; background: #fdba74; display: inline-flex; align-items: center; }
#sync-status { width: var(--control-h); height: var(--control-h); display: inline-grid; place-items: center; border-radius: 8px; background: #9ca3af; color: #ffffff; font-size: 18px; font-weight: 700; }
#sync-status.status-active { background: #f59e0b; animation: sync-blink 1s ease-in-out infinite alternate; }
#sync-status.status-ok { background: #10b981; }
#sync-status.status-error { background: #ef4444; animation: warning-blink 1s ease-in-out infinite alternate; }
#sync-status.status-saved { background: #10b981; }
#sync-status.status-active .icon { animation: sync-icon-blink 1s ease-in-out infinite alternate; }
#sync-status.status-error .icon { animation: warning-icon-blink-inverse 1s ease-in-out infinite alternate; }
#sync-status.status-warning { background: #ef4444; animation: warning-blink 1s ease-in-out infinite alternate; }
#sync-status.status-warning .icon { animation: warning-icon-blink 1s ease-in-out infinite alternate; }
@keyframes sync-blink { from { opacity: 0.6; } to { opacity: 1; } }
@keyframes warning-blink { from { opacity: 1; } to { opacity: 0.1; } }
@keyframes sync-icon-blink { from { opacity: 1; } to { opacity: 0.6; } }
@keyframes warning-icon-blink { from { opacity: 0.1; } to { opacity: 1; } }
@keyframes warning-icon-blink-inverse { from { opacity: 1; } to { opacity: 0.1; } }
@keyframes warning-button-blink { from { opacity: 1; } to { opacity: 0.1; } }

#breadcrumbs { margin: 10px 12px; color: #6b7280; font-size: 14px; font-weight: 700; }
#breadcrumbs .sep { margin: 0 6px; }
#stats-bar { margin: 0 12px 4px; }
.stats-bar-inner { display: flex; flex-wrap: nowrap; gap: 4px; align-items: stretch; overflow-x: auto; padding-bottom: 4px; }
.sb-item { background: #ffffff; border: 1px solid #e5e7eb; border-radius: 12px; padding: 6px 8px; display: flex; flex-direction: column; justify-content: center; align-items: center; min-height: 56px; text-align: center; white-space: nowrap; flex: 0 0 auto; width: max-content; }
@media (max-width: 768px) {
  .sb-item { padding: 5px 8px; }
}
.sb-title { background: #0ea5e9; color: white; border-color: #0ea5e9; }
.sb-k { font-size: 12px; color: #6b7280; margin-bottom: 2px; }
.sb-v { font-size: 14px; font-weight: 600; color: #111827; }
.sb-good { color: #16a34a; }
.sb-bad { color: #dc2626; }
.sb-daily { flex-direction: row; gap: 8px; justify-content: center; }
.sb-col { display: flex; flex-direction: column; align-items: center; padding: 0 2px; }

/* Modal danger buttons */
.modal-actions .danger { background: #ef4444; color: #fff; border: 1px solid #ef4444; }

/* Auth overlay for passcode */
#auth-overlay { position: fixed; inset: 0; background: rgba(0,0,0,0.65); z-index: 9999; display: grid; place-items: center; }
#auth-overlay.hidden { display: none !important; }
.auth-box { width: min(420px, 92vw); background: #1f2937; border: 1px solid #374151; border-radius: 16px; padding: 20px; box-shadow: 0 10px 24px rgba(0,0,0,0.28); display: flex; flex-direction: column; gap: 16px; }
.auth-title { font-size: 22px; font-weight: 800; color: #ffffff; text-align: center; }
.auth-message { color: #ef4444; text-align: center; font-size: 14px; min-height: 20px; }
.auth-input { padding: 12px 14px; border-radius: 12px; border: 1px solid #374151; background: #ffffff; text-align: center; font-size: 18px; letter-spacing: 4px; width: 100%; }
.auth-keypad { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; margin: 10px 0; }
.auth-keypad button { padding: 15px; border: 1px solid #4b5563; background: #374151; color: #ffffff; border-radius: 12px; font-size: 18px; font-weight: 600; cursor: pointer; transition: background-color 0.2s; }
.auth-keypad button:hover { background: #4b5563; }
.auth-keypad button:active { background: #6b7280; }
.auth-submit { padding: 12px 20px; border-radius: 12px; border: 1px solid #10b981; background: #10b981; color: #ffffff; font-size: 16px; font-weight: 600; cursor: pointer; transition: background-color 0.2s; }
.auth-submit:hover { background: #059669; }
.auth-submit:active { background: #047857; }

/* Settings modal */
.settings-wrap { display: grid; gap: 10px; }
.set-row { display: grid; grid-template-columns: 1fr; gap: 10px; }
.set-col { display: grid; grid-template-columns: auto auto; align-items: center; gap: 8px; justify-content: space-between; }
.set-k { font-weight: 600; color: #374151; }
.settings-wrap input[type="number"],
.settings-wrap input[type="date"] {
  padding: 8px 14px; /* more side space */
  border-radius: 8px;
  border: 1px solid #cbd5e1;
  background: #ffffff;
  width: fit-content;
  min-width: 12ch; /* ensure long numbers like 30000 fit */
  justify-self: end;
}
.settings-wrap .danger { background: #ef4444; color: #fff; border: 1px solid #ef4444; border-radius: 8px; padding: 8px 12px; }
@media (max-width: 768px) {
  .set-row { grid-template-columns: 1fr; }
}

/* Priority graph */
#priority-graph { margin: 0 12px 16px; background: #f1f5f9; border: 1px solid #cbd5e1; border-radius: 12px; padding: 12px; grid-column: 1 / 2; }
.pg-title { font-weight: 700; margin-bottom: 8px; color: #374151; }
.pg-list { display: flex; flex-direction: column; gap: 8px; }
.pg-row { display: grid; grid-template-columns: 200px 1fr 180px; gap: 20px; align-items: center; }
.pg-name { color: #374151; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
/* Center the progress bar within its column with equal side spacing */
.pg-bar { position: relative; height: 16px; background: #f3f4f6; border-radius: 8px; overflow: hidden; border: 1px solid #e5e7eb; width: min(640px, 100%); margin: 0 auto; }
.pg-fill { position: absolute; left: 0; top: 0; bottom: 0; width: 0%; background: #10b981; }
.pg-meta { text-align: right; color: #6b7280; white-space: nowrap; padding-right: 8px; }

@media (max-width: 768px) {
  .pg-row { grid-template-columns: 1fr auto auto; gap: 12px; align-items: center; }
  .pg-bar { display: none; }
  .pg-meta { display: block; padding-right: 0; }
  .pg-percent { display: block; text-align: center; font-weight: 600; white-space: nowrap; }
}
.pg-percent { display: none; white-space: nowrap; }
.pct-red { color: #dc2626; }
.pct-yellow { color: #f59e0b; }
.pct-green { color: #10b981; }

#main { display: grid; grid-template-columns: 1fr; gap: 12px; padding: 12px; }
#main.with-editor { grid-template-columns: 1fr 360px; }
#folder-list { background: #f1f5f9; border: 1px solid #cbd5e1; border-radius: 12px; padding: 12px; min-height: 60vh; max-height: calc(100vh - 160px); overflow: auto; }
#editor-panel { background: #f1f5f9; border: 1px solid #cbd5e1; border-radius: 12px; padding: 12px; max-height: calc(100vh - 160px); overflow: auto; }

/* Tree */
.tree { list-style: none; display: flex; flex-direction: column; gap: 10px; }
.tree li { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 12px; border: 1px solid #d1d5db; border-radius: 12px; background: #f8fafc; box-shadow: 0 1px 0 rgba(0,0,0,0.03); }
.tree li:hover { border-color: #9ca3af; box-shadow: 0 2px 8px rgba(0,0,0,0.06); }
.tree .item-left { display: flex; align-items: center; gap: 12px; }
.tree .name { font-weight: 700; color: #111827; margin-bottom: 2px; }
.tree .meta { display: flex; flex-direction: column; gap: 1px; margin-top: 2px; }
.tree .meta-qty { font-size: 12px; color: #6b7280; }
.tree .meta-value { font-size: 12px; color: #374151; font-weight: 600; }
.tree .text-col { display: flex; flex-direction: column; justify-content: center; }
.tree .icon-box { width: 72px; height: 72px; border-radius: 12px; background: #f1f2f6; display: grid; place-items: center; color: #6b7280; flex: 0 0 auto; border: 1px solid #cbd5e1; overflow: hidden; }
.tree .icon-box img { width: 100%; height: 100%; object-fit: cover; border-radius: 10px; border: 1px solid #cbd5e1; }
.tree .actions { display: flex; gap: 6px; align-items: center; }
.tree .actions button { padding: 6px 10px; border-radius: 8px; border: 1px solid #d1d5db; background: #f3f4f6; }
.tree .actions button:hover { background: #e5e7eb; }

/* Icon boxes and images: add the same grey border as cards */
/* (merged into the earlier definition above; keep sizes consistent) */
.tree .icon-box { width: 72px; height: 72px; border-radius: 12px; background: #f1f2f6; display: grid; place-items: center; color: #6b7280; flex: 0 0 auto; border: 1px solid #cbd5e1; overflow: hidden; }
.tree .icon-box img { width: 100%; height: 100%; object-fit: cover; border-radius: 10px; border: 1px solid #cbd5e1; }

/* Editor */
.editor-header { display: flex; align-items: center; justify-content: space-between; margin-bottom: 10px; }
#editor-form { display: flex; flex-direction: column; gap: 12px; }
#editor-form label { display: flex; flex-direction: column; gap: 6px; font-size: 14px; }
#editor-form input[type="text"],
#editor-form input[type="number"] { padding: 8px 10px; border-radius: 8px; border: 1px solid #d1d5db; }
.editor-actions { display: flex; gap: 8px; }
.editor-actions .danger { background: #ef4444; color: white; border: 1px solid #ef4444; }
.editor-actions button { padding: 8px 12px; border-radius: 8px; border: 1px solid #0ea5e9; background: #0ea5e9; color: #fff; }

/* Custom fields */
.custom-fields { border-top: 1px dashed #e5e7eb; padding-top: 8px; }
.custom-fields-header { display: flex; align-items: center; justify-content: space-between; margin-bottom: 8px; }
.custom-field-row { display: grid; grid-template-columns: 1fr 1fr auto; gap: 8px; margin-bottom: 6px; }
.custom-field-row input { padding: 6px 8px; border-radius: 8px; border: 1px solid #d1d5db; }
.custom-field-row button { padding: 6px 8px; border-radius: 8px; border: 1px solid #d1d5db; background: #f3f4f6; }

/* Image preview */
#folder-image-preview { border: 1px dashed #d1d5db; border-radius: 8px; padding: 8px; display: grid; place-items: center; }
#folder-image-preview img { max-width: 100%; border-radius: 6px; }

/* Search */
#search-bar { position: relative; display: flex; }
#search-bar input { width: 360px; max-width: 60vw; height: var(--control-h); padding: 0 10px; border-radius: 8px; border: 1px solid #d1d5db; }
.dropdown { position: absolute; top: 110%; left: 0; right: 0; background: #ffffff; border: 1px solid #e5e7eb; border-radius: 10px; box-shadow: 0 10px 30px rgba(0,0,0,0.08); max-height: 320px; overflow: auto; }
.dropdown .result { padding: 8px 10px; border-bottom: 1px solid #f3f4f6; cursor: pointer; }
.dropdown .result:hover { background: #f3f4f6; }

/* Search toggle button (mobile) */
#search-toggle { display: inline-flex; align-items: center; justify-content: center; padding: 0 10px; border-radius: 8px; border: 1px solid #d1d5db; background: #ffffff; cursor: pointer; height: var(--control-h); width: var(--control-h); }

/* Collapsed/expanded states */
#search-bar.search-collapsed { display: none; }
#search-bar.search-expanded { display: flex; }

/* Mobile portrait view */
@media (max-width: 768px) and (orientation: portrait) {
  #search-bar { display: none; }
  #search-toggle { display: inline-flex; }
  /* When expanded, show the bar as overlay */
  #search-bar.search-expanded { display: flex; position: absolute; top: 100%; left: 0; right: 0; background: #fff; padding: 8px; z-index: 20; }
}

/* Modal */
#modal { position: fixed; inset: 0; background: rgba(0,0,0,0.35); display: grid; place-items: center; z-index: 100; }

/* Top bar buttons */
.btn-search { height: var(--control-h); padding: 0 10px; border-radius: 8px; border: 1px solid #d1d5db; background: #ffffff; cursor: pointer; display: inline-flex; align-items: center; }
#settings-btn { height: var(--control-h); padding: 0 10px; border-radius: 8px; border: 1px solid #d1d5db; background: #ffffff; display: inline-flex; align-items: center; }

/* Icon size */
.btn-search .icon, #settings-btn .icon { width: calc(var(--control-h) - 12px); height: calc(var(--control-h) - 12px); display: block; }

/* Product page target quantity */
#pp-target { color: #ef4444; }

#modal.hidden { display: none !important; }
.modal-content { width: min(560px, 92vw); background: #ffffff; border-radius: 12px; border: 1px solid #e5e7eb; overflow: hidden; }
.modal-header { display: flex; align-items: center; justify-content: space-between; padding: 10px 12px; border-bottom: 1px solid #f3f4f6; }
#modal-body { padding: 12px; }
.modal-actions { display: flex; gap: 8px; padding: 12px; border-top: 1px solid #f3f4f6; justify-content: flex-end; }
.modal-actions button { padding: 8px 10px; border-radius: 8px; border: 1px solid #d1d5db; background: #0ea5e9; color: white; }

/* Toasts */
#toast-container { position: fixed; bottom: 16px; right: 16px; display: flex; flex-direction: column; gap: 8px; z-index: 50; }
.toast { background: #111827; color: #ffffff; padding: 10px 12px; border-radius: 10px; opacity: 0.95; animation: fadeout 0.3s ease-in both; }

/* Bigger back/close buttons */
#pp-back, #editor-close, #modal-close { padding: 6px 12px; border-radius: 8px; border: 1px solid #d1d5db; background: #ffffff; cursor: pointer; font-size: 14px; line-height: 1; }
#pp-back:hover, #editor-close:hover, #modal-close:hover { background: #f3f4f6; }
#pp-back:active, #editor-close:active, #modal-close:active { background: #e5e7eb; }

/* Clickable tiles */
.tree .item-left { cursor: pointer; }

/* Drag & drop reorder visuals */
.dragging { opacity: 0.6; }
.drop-before { position: relative; }
.drop-before::before { content: ""; position: absolute; left: 0; right: 0; top: -4px; height: 0; border-top: 3px solid #0ea5e9; border-radius: 2px; }

/* Refresh overlay */
#refresh-overlay { position: fixed; inset: 0; display: none; align-items: center; justify-content: center; background: rgba(0,0,0,0.6); z-index: 200; }
#refresh-overlay .box { background: #111827; color: #fff; padding: 20px 24px; border-radius: 12px; font-size: 18px; box-shadow: 0 10px 30px rgba(0,0,0,0.4); }
#refresh-overlay.show { display: flex; }
@keyframes fadeout { from { opacity: 0.95; } to { opacity: 0; } }

/* Responsive */
@media (max-width: 768px) {
  .stats-bar-inner { grid-auto-columns: 66vw; }
  .set-row { grid-template-columns: 1fr; }
  .report-grid { grid-template-columns: 1fr; }
  #main { grid-template-columns: 1fr; }
  #editor-panel { order: -1; }
  #search-bar input { width: 180px; max-width: 100%; }
}

/* Product Page (overlay) */
#product-page { position: fixed; inset: 0; background: #f7f7f9; z-index: 40; overflow: auto; overscroll-behavior-y: contain; -webkit-overflow-scrolling: touch; height: 100dvh; height: 100vh; }
.pp-header { position: fixed; top: 0; left: 0; right: 0; z-index: 41; display: grid; grid-template-columns: auto 1fr; align-items: center; gap: 6px; padding: 6px 8px; background: #ffffff; border-bottom: 1px solid #e5e7eb; }
.pp-left { display: inline-flex; align-items: center; gap: 8px; }
.pp-back { padding: 6px 8px; border-radius: 8px; border: 1px solid #d1d5db; background: #ffffff; font-size: 14px; }
/* Light yellow edit button */
.pp-edit { padding: 6px 8px; border-radius: 8px; border: 1px solid #fde68a; background: #fef3c7; color: #92400e; font-size: 14px; }
.pp-edit:hover { background: #fde68a; }
.pp-title { font-weight: 800; font-size: 19px; text-align: center; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; justify-self: center; }
.pp-content { padding: 8px; display: grid; gap: 8px; max-width: 960px; margin: 0 auto; padding-top: calc(48px + env(safe-area-inset-top)); padding-bottom: calc(116px + env(safe-area-inset-bottom)); }
.pp-card { background: #ffffff; border: 1px solid #e5e7eb; border-radius: 12px; padding: 8px; }
.pp-card-title { font-weight: 700; margin-bottom: 10px; }
.pp-info-grid { display: grid; }
.pp-row { display: grid; grid-template-columns: 1fr 1fr; padding: 4px 0; border-top: 1px solid #f3f4f6; }
.pp-row:first-child { border-top: 0; }
.pp-label { color: #6b7280; }
.pp-value { text-align: right; font-weight: 600; }
.pp-adjust { display: flex; gap: 8px; align-items: center; }
.pp-adjust-label { font-weight: 600; color: #374151; flex-shrink: 0; margin-right: 4px; }
.pp-adjust input { width: 55px; padding: 6px 8px; border-radius: 8px; border: 1px solid #d1d5db; text-align: center; margin-right: 8px; }
.pp-adjust button { padding: 6px 8px; border-radius: 8px; border: 1px solid #d1d5db; background: #0ea5e9; color: white; flex-shrink: 0; }
.pp-adjust .danger { background: #ef4444; border-color: #ef4444; }
.pp-image { display: flex; gap: 8px; align-items: center; }
.pp-image-preview { width: 72px; height: 72px; object-fit: cover; border-radius: 10px; border: 1px solid #e5e7eb; background: #f1f2f6; }

/* Floating back button (always accessible) */
.pp-fab-back { position: fixed; right: 12px; bottom: 20px; z-index: 100; padding: 10px 12px; border-radius: 9999px; border: 1px solid #d1d5db; background: #111827; color: #ffffff; font-weight: 700; box-shadow: 0 6px 18px rgba(0,0,0,0.2); }
.pp-fab-back:active { transform: translateY(1px); }

/* Product note */
.pp-note textarea { width: 100%; padding: 8px 10px; border-radius: 10px; border: 1px solid #d1d5db; resize: none; overflow: hidden; min-height: 36px; line-height: 1.4; }
.pp-note-actions { margin-top: 8px; }
.pp-note-actions button { padding: 8px 12px; border-radius: 8px; border: 1px solid #0ea5e9; background: #0ea5e9; color: #fff; }

/* Independent folder styling */
.tree li.independent-folder { background: #cbd5e1; border: 1px solid #94a3b8; border-radius: 12px; }
.tree li.independent-folder:hover { background: #b0bcc4; border-color: #64748b; }
.tree li.independent-folder .name { color: #7c3aed; font-weight: 700; }
.tree li.independent-folder .meta-qty, .tree li.independent-folder .meta-value { display: none !important; }
/* Ensure products don't inherit independent folder styling */
.tree li:not(.independent-folder) { background: #f8fafc; }
.tree li:not(.independent-folder) .name { color: #111827; }
.tree li:not(.independent-folder) .meta-qty, .tree li:not(.independent-folder) .meta-value { display: flex !important; }

/* Home stats card */
.stats-card { display: flex; align-items: center; justify-content: space-between; padding: 14px; border: 1px dashed #e5e7eb; border-radius: 12px; background: #f9fafb; }
.stats-card .title { font-weight: 700; }
.stats-card .values { display: flex; gap: 16px; color: #374151; }
