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

:root {
  --bg: #0f0f1a;
  --card-bg: #1a1a2e;
  --card-border: #2a2a40;
  --text: #e0e0e0;
  --text-dim: #8888a0;
  --accent-blue: #4fc3f7;
  --accent-green: #81c784;
  --accent-yellow: #ffd54f;
  --accent-orange: #ffb74d;
  --accent-red: #ff8a65;
  --radius: 12px;
}

html {
  font-size: 16px;
  -webkit-text-size-adjust: 100%;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  min-height: 100vh;
  padding-bottom: env(safe-area-inset-bottom);
}

/* Header */
header {
  position: sticky;
  top: 0;
  z-index: 100;
  background: var(--card-bg);
  border-bottom: 1px solid var(--card-border);
  padding: 12px 16px;
  display: flex;
  align-items: center;
  justify-content: space-between;
}

header h1 {
  font-size: 1.25rem;
  font-weight: 700;
  color: var(--accent-blue);
}

.header-meta {
  display: flex;
  align-items: center;
  gap: 10px;
}

#last-updated {
  font-size: 0.75rem;
  color: var(--text-dim);
}

#refresh-btn {
  background: none;
  border: 1px solid var(--card-border);
  color: var(--accent-blue);
  font-size: 1.25rem;
  width: 36px;
  height: 36px;
  border-radius: 8px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
}

#refresh-btn:active {
  background: var(--card-border);
}

#refresh-btn.spinning {
  animation: spin 0.6s linear;
}

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

/* Main */
main {
  padding: 12px;
  max-width: 900px;
  margin: 0 auto;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

/* Cards */
.card {
  background: var(--card-bg);
  border: 1px solid var(--card-border);
  border-radius: var(--radius);
  overflow: hidden;
  transition: border-color 0.2s;
}

.card[data-expandable] {
  cursor: pointer;
}

.card[data-expandable]:hover {
  border-color: var(--accent-blue);
}

.card-header {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 16px;
}

.card-icon {
  font-size: 1.5rem;
  flex-shrink: 0;
  width: 40px;
  text-align: center;
}

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

.card-title h2 {
  font-size: 1.3rem;
  font-weight: 700;
  color: var(--text);
}

.card-subtitle {
  font-size: 0.8rem;
  color: var(--text-dim);
}

.card-metric {
  font-size: 1.75rem;
  font-weight: 800;
  color: var(--accent-blue);
  flex-shrink: 0;
  text-align: right;
}

.card-metric.good { color: var(--accent-green); }
.card-metric.warn { color: var(--accent-yellow); }
.card-metric.bad { color: var(--accent-orange); }

/* Card body (expandable) */
.card-body {
  max-height: 0;
  overflow: hidden;
  transition: max-height 0.3s ease;
  border-top: 0px solid transparent;
}

.card.expanded .card-body {
  max-height: 2000px;
  border-top: 1px solid var(--card-border);
}

/* Inventory list items */
.inv-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 16px;
  border-bottom: 1px solid var(--card-border);
}

.inv-item:last-child { border-bottom: none; }

.inv-item-name {
  font-size: 0.9rem;
  font-weight: 500;
  flex: 1;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.inv-item-qty {
  font-size: 0.9rem;
  font-weight: 700;
  margin-left: 12px;
  white-space: nowrap;
}

.inv-item-status {
  font-size: 0.75rem;
  font-weight: 600;
  padding: 2px 8px;
  border-radius: 6px;
  margin-left: 8px;
  white-space: nowrap;
}

.status-ok { background: rgba(79,195,247,0.15); color: var(--accent-blue); }
.status-watch { background: rgba(255,213,79,0.2); color: var(--accent-yellow); }
.status-low { background: rgba(255,183,77,0.2); color: var(--accent-orange); }
.status-critical { background: rgba(255,138,101,0.25); color: #ff8a65; }
.status-nodata { background: rgba(136,136,160,0.15); color: var(--text-dim); }

/* Inventory sub-sections */
.inv-section {
  border-bottom: 1px solid var(--card-border);
}

.inv-section:last-child {
  border-bottom: none;
}

.inv-section-header {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 12px 16px;
  cursor: pointer;
  background: rgba(255,255,255,0.02);
  transition: background 0.15s;
}

.inv-section-header:hover {
  background: rgba(255,255,255,0.05);
}

.inv-section-title {
  font-size: 0.95rem;
  font-weight: 700;
  color: var(--text);
  flex: 1;
}

.inv-section-count {
  font-size: 0.75rem;
  color: var(--text-dim);
  white-space: nowrap;
}

.inv-section-health {
  font-size: 0.75rem;
  font-weight: 700;
  padding: 2px 8px;
  border-radius: 6px;
  white-space: nowrap;
}

.inv-section-chevron {
  font-size: 0.7rem;
  color: var(--text-dim);
  transition: transform 0.2s;
}

.inv-section.section-expanded .inv-section-chevron {
  transform: rotate(90deg);
}

.inv-section-body {
  max-height: 0;
  overflow: hidden;
  transition: max-height 0.3s ease;
}

.inv-section.section-expanded .inv-section-body {
  max-height: 2000px;
}

/* Inventory item detail column */
.inv-item-detail {
  font-size: 0.75rem;
  color: var(--text-dim);
  margin-left: 8px;
  white-space: nowrap;
  flex-shrink: 0;
}

/* Days remaining badge */
.inv-item-days {
  font-size: 0.75rem;
  font-weight: 700;
  color: var(--accent-yellow);
  margin-left: 8px;
  white-space: nowrap;
  flex-shrink: 0;
}

/* Sales periods (Day / Month / Year) */
.sales-periods {
  display: flex;
  gap: 1px;
  background: var(--card-border);
}

.sales-period {
  flex: 1;
  background: var(--card-bg);
  padding: 14px 12px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
  text-align: center;
}

.sales-period-label {
  font-size: 0.7rem;
  color: var(--text-dim);
  text-transform: uppercase;
  letter-spacing: 0.5px;
}

.sales-period-revenue {
  font-size: 1.2rem;
  font-weight: 800;
  color: var(--accent-blue);
}

.sales-period-orders {
  font-size: 0.75rem;
  color: var(--text-dim);
}

/* Sales stats */
.sales-stats {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 1px;
  background: var(--card-border);
}

.sales-stat {
  background: var(--card-bg);
  padding: 12px 16px;
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.sales-stat-label {
  font-size: 0.7rem;
  color: var(--text-dim);
  text-transform: uppercase;
  letter-spacing: 0.5px;
}

.sales-stat-value {
  font-size: 1.1rem;
  font-weight: 700;
  color: var(--text);
}

.sales-daily-title {
  padding: 12px 16px 4px;
  font-size: 0.75rem;
  color: var(--text-dim);
  text-transform: uppercase;
  letter-spacing: 0.5px;
}

.sales-daily {
  padding: 4px 16px 12px;
}

.sales-day {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 4px 0;
}

.sales-day-label {
  font-size: 0.75rem;
  color: var(--text-dim);
  width: 70px;
  flex-shrink: 0;
}

.sales-day-bar-bg {
  flex: 1;
  height: 8px;
  background: rgba(255,255,255,0.05);
  border-radius: 4px;
  overflow: hidden;
}

.sales-day-bar {
  height: 100%;
  background: var(--accent-blue);
  border-radius: 4px;
  transition: width 0.3s;
}

.sales-day-val {
  font-size: 0.75rem;
  font-weight: 600;
  color: var(--text);
  width: 65px;
  text-align: right;
  flex-shrink: 0;
}

/* PO items */
.po-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 16px;
  border-bottom: 1px solid var(--card-border);
}

.po-item:last-child { border-bottom: none; }

.po-label {
  font-size: 0.9rem;
  flex: 1;
}

.po-status {
  font-size: 0.75rem;
  font-weight: 600;
  padding: 2px 8px;
  border-radius: 6px;
}

.po-draft { background: rgba(79,195,247,0.2); color: var(--accent-blue); }
.po-test { background: rgba(206,147,216,0.2); color: #ab47bc; }
.po-sent { background: rgba(255,213,79,0.2); color: var(--accent-yellow); }
.po-confirmed { background: rgba(100,181,246,0.2); color: #42a5f5; }
.po-received { background: rgba(129,199,132,0.2); color: var(--accent-green); }

.po-pdf-link {
  text-decoration: none;
  margin-right: 8px;
  font-size: 1.1rem;
  cursor: pointer;
}
.po-pdf-link:hover { opacity: 0.7; }
.po-pdf-download {
  text-decoration: none;
  margin-right: 8px;
  font-size: 0.9rem;
  cursor: pointer;
}
.po-pdf-download:hover { opacity: 0.7; }

/* Inquiry items */
.inq-item {
  padding: 12px 0;
  border-bottom: 1px solid #2a2a3e;
}
.inq-item:last-child { border-bottom: none; }
.inq-pending { border-left: 3px solid #eab308; padding-left: 12px; }
.inq-replied { border-left: 3px solid #22c55e; padding-left: 12px; opacity: 0.8; }
.inq-header {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 4px;
}
.inq-status { font-size: 1rem; }
.inq-name { font-weight: 600; color: #e0e0e0; flex: 1; }
.inq-date { font-size: 0.8rem; color: #888; }
.inq-meta { font-size: 0.8rem; color: #999; margin-bottom: 4px; padding-left: 28px; }
.inq-preview {
  font-size: 0.85rem;
  color: #bbb;
  padding-left: 28px;
  margin: 4px 0;
  line-height: 1.4;
}
.inq-actions {
  padding-left: 28px;
  margin-top: 6px;
  display: flex;
  gap: 12px;
  align-items: center;
}
.inq-link {
  font-size: 0.8rem;
  color: #60a5fa;
  text-decoration: none;
}
.inq-link:hover { text-decoration: underline; }
.inq-thread-count {
  font-size: 0.75rem;
  color: #888;
  background: #2a2a3e;
  padding: 2px 8px;
  border-radius: 10px;
}

/* Project items */
.proj-item {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px 16px;
  border-bottom: 1px solid var(--card-border);
}

.proj-item:last-child { border-bottom: none; }

.proj-name {
  flex: 1;
  font-size: 0.9rem;
  font-weight: 500;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.proj-owner {
  font-size: 0.75rem;
  color: var(--text-dim);
  white-space: nowrap;
}

.proj-priority {
  font-size: 0.7rem;
  font-weight: 700;
  padding: 2px 6px;
  border-radius: 4px;
  white-space: nowrap;
}

.priority-high { background: rgba(255,183,77,0.2); color: var(--accent-orange); }
.priority-medium { background: rgba(255,213,79,0.2); color: var(--accent-yellow); }
.priority-low { background: rgba(129,199,132,0.2); color: var(--accent-green); }

.proj-status-badge {
  font-size: 0.7rem;
  font-weight: 600;
  padding: 2px 6px;
  border-radius: 4px;
  white-space: nowrap;
}

.pstat-active { background: rgba(79,195,247,0.2); color: var(--accent-blue); }
.pstat-done { background: rgba(129,199,132,0.2); color: var(--accent-green); }
.pstat-blocked { background: rgba(255,183,77,0.2); color: var(--accent-orange); }
.pstat-planned { background: rgba(136,136,160,0.2); color: var(--text-dim); }

/* Badges for placeholder cards */
.badge {
  font-size: 0.7rem;
  font-weight: 700;
  padding: 4px 10px;
  border-radius: 6px;
  flex-shrink: 0;
}

.badge-soon {
  background: rgba(255,213,79,0.15);
  color: var(--accent-yellow);
}

.badge-planned {
  background: rgba(136,136,160,0.15);
  color: var(--text-dim);
}

.card-placeholder {
  opacity: 0.6;
}

/* Error / empty */
.card-empty {
  padding: 16px;
  text-align: center;
  color: var(--text-dim);
  font-size: 0.85rem;
}

/* Desktop: 2-column grid */
@media (min-width: 640px) {
  main {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 8px;
  }

  #card-projects {
    grid-column: 1 / -1;
  }
}
