:root {
  --bg: #f4f6f8;
  --surface: #ffffff;
  --surface-alt: #f8fafc;
  --text: #0f172a;
  --text-muted: #475569;
  --border: #cbd5e1;
  --border-soft: #e2e8f0;
  --accent: #ea580c;
  --accent-hover: #c2410c;
  --accent-soft: #ffedd5;
  --accent-text: #9a3412;
  --box-bg: #1e293b;
  --box-text: #ffffff;
  --danger: #dc2626;
  --danger-soft: #fee2e2;
  --success: #16a34a;
  --success-soft: #dcfce7;
  --warning-bg: #fef3c7;
  --warning-text: #92400e;
  --shadow: 0 2px 8px rgba(15, 23, 42, 0.08);
  --shadow-lg: 0 12px 32px rgba(15, 23, 42, 0.18);
  --radius: 12px;
  --radius-sm: 8px;
  --nav-height: 64px;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root[data-theme="system"] {
    --bg: #0b1120;
    --surface: #1e293b;
    --surface-alt: #0f172a;
    --text: #f1f5f9;
    --text-muted: #94a3b8;
    --border: #334155;
    --border-soft: #263348;
    --accent: #f97316;
    --accent-hover: #fb923c;
    --accent-soft: #431407;
    --accent-text: #fdba74;
    --box-bg: #f97316;
    --box-text: #0f172a;
    --danger: #ef4444;
    --danger-soft: #450a0a;
    --success: #22c55e;
    --success-soft: #052e16;
    --warning-bg: #451a03;
    --warning-text: #fde68a;
    --shadow: 0 2px 10px rgba(0, 0, 0, 0.4);
    --shadow-lg: 0 14px 36px rgba(0, 0, 0, 0.6);
    color-scheme: dark;
  }
}

:root[data-theme="dark"] {
  --bg: #0b1120;
  --surface: #1e293b;
  --surface-alt: #0f172a;
  --text: #f1f5f9;
  --text-muted: #94a3b8;
  --border: #334155;
  --border-soft: #263348;
  --accent: #f97316;
  --accent-hover: #fb923c;
  --accent-soft: #431407;
  --accent-text: #fdba74;
  --box-bg: #f97316;
  --box-text: #0f172a;
  --danger: #ef4444;
  --danger-soft: #450a0a;
  --success: #22c55e;
  --success-soft: #052e16;
  --warning-bg: #451a03;
  --warning-text: #fde68a;
  --shadow: 0 2px 10px rgba(0, 0, 0, 0.4);
  --shadow-lg: 0 14px 36px rgba(0, 0, 0, 0.6);
  color-scheme: dark;
}

* {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  padding: 0;
  min-height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  line-height: 1.45;
  -webkit-tap-highlight-color: transparent;
}

.hidden {
  display: none !important;
}

.offline-banner {
  background: var(--warning-bg);
  color: var(--warning-text);
  text-align: center;
  padding: 10px 16px;
  font-size: 0.9rem;
  font-weight: 600;
  border-bottom: 1px solid var(--border);
}

/* Header */
.app-header {
  position: sticky;
  top: 0;
  z-index: 30;
  background: var(--surface);
  border-bottom: 1px solid var(--border-soft);
  box-shadow: var(--shadow);
}

.header-inner {
  max-width: 880px;
  margin: 0 auto;
  padding: 8px 14px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

.brand {
  display: inline-flex;
  align-items: center;
  gap: 10px;
  text-decoration: none;
  color: var(--text);
  font-weight: 800;
  font-size: 1.12rem;
  min-height: 44px;
}

.top-nav {
  display: none;
  align-items: center;
  gap: 6px;
}

.nav-link {
  text-decoration: none;
  color: var(--text-muted);
  font-weight: 600;
  padding: 10px 14px;
  border-radius: var(--radius-sm);
  min-height: 44px;
  display: inline-flex;
  align-items: center;
}

.nav-link.active {
  background: var(--accent-soft);
  color: var(--accent-text);
}

/* Bottom Nav (Mobile first) */
.bottom-nav {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  z-index: 30;
  background: var(--surface);
  border-top: 1px solid var(--border-soft);
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  padding-bottom: env(safe-area-inset-bottom, 0px);
}

.bottom-nav-link {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
  min-height: 58px;
  text-decoration: none;
  color: var(--text-muted);
  font-size: 0.78rem;
  font-weight: 600;
}

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

.nav-icon {
  font-size: 1.15rem;
  line-height: 1;
}

@media (min-width: 720px) {
  .top-nav {
    display: flex;
  }
  .bottom-nav {
    display: none;
  }
}

/* Main container */
.app-main {
  max-width: 880px;
  margin: 0 auto;
  padding: 16px 14px 104px;
}

@media (min-width: 720px) {
  .app-main {
    padding-bottom: 92px;
  }
}

/* Buttons & Touch Targets */
button,
.btn {
  font: inherit;
  cursor: pointer;
  border: 1px solid transparent;
  border-radius: var(--radius-sm);
  min-height: 44px;
  min-width: 44px;
  padding: 10px 16px;
  font-weight: 600;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  text-decoration: none;
  transition: background-color 0.15s ease, opacity 0.15s ease, transform 0.08s ease;
}

button:active:not(:disabled),
.btn:active:not(:disabled) {
  transform: scale(0.98);
}

button:disabled,
.btn:disabled {
  opacity: 0.45;
  cursor: not-allowed;
}

.btn-primary {
  background: var(--accent);
  color: #ffffff;
}
.btn-primary:hover:not(:disabled) {
  background: var(--accent-hover);
}

.btn-secondary {
  background: var(--surface);
  color: var(--text);
  border-color: var(--border);
}
.btn-secondary:hover:not(:disabled) {
  background: var(--surface-alt);
}

.btn-danger {
  background: var(--danger);
  color: #ffffff;
}

.btn-ghost {
  background: transparent;
  color: var(--text-muted);
}

.icon-btn {
  background: var(--surface-alt);
  color: var(--text);
  border: 1px solid var(--border-soft);
  padding: 8px 12px;
  border-radius: var(--radius-sm);
  font-size: 0.92rem;
}

.fab-btn {
  position: fixed;
  right: 18px;
  bottom: calc(74px + env(safe-area-inset-bottom, 0px));
  z-index: 35;
  width: 58px;
  height: 58px;
  border-radius: 50%;
  background: var(--accent);
  color: #ffffff;
  font-size: 2rem;
  font-weight: 500;
  line-height: 1;
  box-shadow: var(--shadow-lg);
}

@media (min-width: 720px) {
  .fab-btn {
    bottom: 28px;
    right: max(24px, calc((100vw - 880px) / 2));
  }
}

/* Search bar */
.search-box-wrap {
  position: relative;
  margin-bottom: 12px;
}

.search-input {
  width: 100%;
  min-height: 54px;
  padding: 12px 52px 12px 18px;
  font-size: 1.08rem;
  font-weight: 500;
  border-radius: var(--radius);
  border: 2px solid var(--border);
  background: var(--surface);
  color: var(--text);
  box-shadow: var(--shadow);
}

.search-input:focus {
  outline: none;
  border-color: var(--accent);
}

.search-clear-btn {
  position: absolute;
  right: 6px;
  top: 50%;
  transform: translateY(-50%);
  width: 44px;
  height: 44px;
  padding: 0;
  border: none;
  background: transparent;
  color: var(--text-muted);
  font-size: 1.35rem;
}

.status-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  margin-bottom: 12px;
  color: var(--text-muted);
  font-size: 0.9rem;
}

/* Cards & Lists */
.card {
  background: var(--surface);
  border: 1px solid var(--border-soft);
  border-radius: var(--radius);
  padding: 16px;
  box-shadow: var(--shadow);
}

.stack {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.item-card {
  display: flex;
  align-items: center;
  gap: 14px;
  padding: 12px;
  background: var(--surface);
  border: 1px solid var(--border-soft);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  text-decoration: none;
  color: var(--text);
  min-height: 76px;
  cursor: pointer;
}

.item-card:hover {
  border-color: var(--accent);
}

.item-thumb {
  width: 60px;
  height: 60px;
  border-radius: var(--radius-sm);
  object-fit: cover;
  background: var(--surface-alt);
  border: 1px solid var(--border-soft);
  flex-shrink: 0;
}

.item-thumb-placeholder {
  width: 60px;
  height: 60px;
  border-radius: var(--radius-sm);
  background: var(--surface-alt);
  border: 1px solid var(--border-soft);
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--text-muted);
  font-size: 1.4rem;
  flex-shrink: 0;
}

.item-card-body {
  flex: 1;
  min-width: 0;
}

.item-card-title {
  font-size: 1.04rem;
  font-weight: 700;
  margin: 0 0 6px;
  word-break: break-word;
}

.item-meta-row {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 6px;
}

.box-badge {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  background: var(--box-bg);
  color: var(--box-text);
  font-weight: 700;
  font-size: 0.86rem;
  padding: 4px 10px;
  border-radius: 999px;
  max-width: 100%;
}

.box-badge-name {
  font-weight: 500;
  opacity: 0.9;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.meta-pill {
  display: inline-flex;
  align-items: center;
  background: var(--surface-alt);
  border: 1px solid var(--border-soft);
  color: var(--text-muted);
  font-size: 0.8rem;
  font-weight: 600;
  padding: 3px 9px;
  border-radius: 999px;
}

/* Boxes grid */
.box-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(230px, 1fr));
  gap: 12px;
}

.box-card {
  background: var(--surface);
  border: 1px solid var(--border-soft);
  border-radius: var(--radius);
  padding: 16px;
  box-shadow: var(--shadow);
  text-decoration: none;
  color: var(--text);
  display: flex;
  flex-direction: column;
  gap: 8px;
  min-height: 92px;
}

.box-card:hover {
  border-color: var(--accent);
}

.box-card-top {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
}

.box-number-pill {
  background: var(--box-bg);
  color: var(--box-text);
  font-weight: 800;
  font-size: 1.05rem;
  padding: 4px 12px;
  border-radius: 999px;
}

.box-card-name {
  font-weight: 600;
  font-size: 0.98rem;
  color: var(--text);
}

/* Page header */
.page-head {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  margin-bottom: 16px;
}

.page-title {
  margin: 0;
  font-size: 1.35rem;
  font-weight: 800;
}

.btn-row {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

/* Forms */
.form-stack {
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.field {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.field label {
  font-size: 0.9rem;
  font-weight: 700;
  color: var(--text);
}

.input,
.select,
.textarea {
  width: 100%;
  min-height: 46px;
  padding: 10px 12px;
  font: inherit;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--surface-alt);
  color: var(--text);
}

.textarea {
  min-height: 86px;
  resize: vertical;
}

.input:focus,
.select:focus,
.textarea:focus {
  outline: none;
  border-color: var(--accent);
}

/* Photo section in item form & detail */
.photo-preview-box {
  display: flex;
  align-items: center;
  gap: 14px;
  flex-wrap: wrap;
}

.photo-preview-img {
  width: 112px;
  height: 112px;
  border-radius: var(--radius);
  object-fit: cover;
  border: 1px solid var(--border);
  background: var(--surface-alt);
}

.detail-photo {
  width: 100%;
  max-width: 320px;
  aspect-ratio: 1 / 1;
  object-fit: cover;
  border-radius: var(--radius);
  border: 1px solid var(--border);
  cursor: zoom-in;
  display: block;
}

/* Segmented theme selector */
.segmented {
  display: inline-grid;
  grid-template-columns: repeat(3, 1fr);
  background: var(--surface-alt);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 3px;
  gap: 3px;
}

.segmented button {
  min-height: 40px;
  background: transparent;
  color: var(--text-muted);
  border: none;
  padding: 6px 12px;
}

.segmented button.active {
  background: var(--surface);
  color: var(--text);
  box-shadow: var(--shadow);
}

/* Modals */
.modal-root {
  position: fixed;
  inset: 0;
  z-index: 60;
  background: rgba(15, 23, 42, 0.58);
  display: flex;
  align-items: flex-end;
  justify-content: center;
  padding: 0;
}

@media (min-width: 600px) {
  .modal-root {
    align-items: center;
    padding: 16px;
  }
}

.modal-card {
  width: 100%;
  max-width: 540px;
  max-height: 92vh;
  overflow-y: auto;
  background: var(--surface);
  color: var(--text);
  border-radius: 16px 16px 0 0;
  padding: 18px;
  box-shadow: var(--shadow-lg);
}

@media (min-width: 600px) {
  .modal-card {
    border-radius: 16px;
  }
}

.modal-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  margin-bottom: 14px;
}

.modal-title {
  margin: 0;
  font-size: 1.15rem;
  font-weight: 800;
}

/* Lightbox */
.lightbox-root {
  position: fixed;
  inset: 0;
  z-index: 80;
  background: rgba(0, 0, 0, 0.92);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
}

.lightbox-root img {
  max-width: 96vw;
  max-height: 90vh;
  border-radius: var(--radius);
  object-fit: contain;
}

.lightbox-close {
  position: absolute;
  top: 16px;
  right: 16px;
  background: rgba(255, 255, 255, 0.16);
  color: #ffffff;
  border: 1px solid rgba(255, 255, 255, 0.35);
}

/* Toast */
.toast-root {
  position: fixed;
  left: 50%;
  transform: translateX(-50%);
  bottom: calc(74px + env(safe-area-inset-bottom, 0px));
  z-index: 90;
  display: flex;
  flex-direction: column;
  gap: 8px;
  width: min(92vw, 440px);
  pointer-events: none;
}

@media (min-width: 720px) {
  .toast-root {
    bottom: 24px;
  }
}

.toast {
  background: #0f172a;
  color: #ffffff;
  padding: 12px 16px;
  border-radius: var(--radius-sm);
  font-weight: 600;
  font-size: 0.92rem;
  box-shadow: var(--shadow-lg);
  pointer-events: auto;
}

.toast.error {
  background: var(--danger);
}

.toast.success {
  background: #15803d;
}

/* Empty state & Alerts */
.empty-state {
  text-align: center;
  padding: 36px 18px;
  background: var(--surface);
  border: 1px dashed var(--border);
  border-radius: var(--radius);
  color: var(--text-muted);
}

.empty-state h3 {
  margin: 0 0 6px;
  color: var(--text);
}

.alert {
  padding: 12px 14px;
  border-radius: var(--radius-sm);
  font-size: 0.92rem;
  font-weight: 600;
}

.alert-danger {
  background: var(--danger-soft);
  color: var(--danger);
}

.alert-warning {
  background: var(--warning-bg);
  color: var(--warning-text);
}

.alert-success {
  background: var(--success-soft);
  color: var(--success);
}

/* QR Print Label */
.qr-label-preview {
  background: #ffffff;
  color: #0f172a;
  border: 2px dashed #94a3b8;
  border-radius: var(--radius);
  padding: 20px;
  text-align: center;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 10px;
}

.qr-svg-wrap svg {
  width: 200px;
  height: 200px;
  display: block;
}

.qr-box-number {
  font-size: 2rem;
  font-weight: 900;
  line-height: 1.1;
}

.qr-box-name {
  font-size: 1.1rem;
  font-weight: 600;
  color: #334155;
}

.qr-url-text {
  font-size: 0.78rem;
  color: #64748b;
  word-break: break-all;
}

.error-list {
  margin: 8px 0 0;
  padding-left: 18px;
  max-height: 160px;
  overflow-y: auto;
  font-size: 0.86rem;
}

@media print {
  body * {
    visibility: hidden !important;
  }
  #print-label-area,
  #print-label-area * {
    visibility: visible !important;
  }
  #print-label-area {
    position: fixed;
    left: 50%;
    top: 24px;
    transform: translateX(-50%);
    border: 2px solid #000000;
    padding: 24px 32px;
    width: 300px;
    background: #ffffff !important;
    color: #000000 !important;
  }
}
