:root {
  --color-primary: #6f42c1;
  --color-primary-dark: #59339d;
  --color-bg: #f5f4f8;
  --color-surface: #ffffff;
  --color-border: #e0dce8;
  --color-text: #241f2b;
  --color-muted: #6b6474;
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-top: env(safe-area-inset-top, 0px);
}

* {
  box-sizing: border-box;
}

html, body {
  height: 100%;
}

body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  background: var(--color-bg);
  color: var(--color-text);
  display: flex;
  flex-direction: column;
}

.app-header {
  padding: calc(0.75rem + var(--safe-top)) 1rem 0.75rem;
  background: var(--color-primary);
  color: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
  position: relative;
}

.app-header h1 {
  margin: 0;
  font-size: 1.25rem;
}

.btn-logout {
  position: absolute;
  right: 1rem;
  top: calc(0.75rem + var(--safe-top));
  background: rgba(255, 255, 255, 0.15);
  color: #fff;
  border: none;
  border-radius: 8px;
  padding: 0.35rem 0.6rem;
  font-size: 0.8rem;
  cursor: pointer;
}

main {
  flex: 1;
  overflow: hidden;
  display: flex;
  flex-direction: column;
  padding: 0.75rem;
  padding-bottom: calc(4.5rem + var(--safe-bottom));
}

.view {
  display: none;
  flex-direction: column;
  flex: 1;
  min-height: 0;
}

.view.active {
  display: flex;
}

.filters {
  display: flex;
  gap: 0.5rem;
  margin-bottom: 0.6rem;
}

.filters input,
.filters select {
  flex: 1;
  padding: 0.6rem 0.7rem;
  border: 1px solid var(--color-border);
  border-radius: 10px;
  font-size: 1rem;
  background: var(--color-surface);
}

.filter-label {
  align-self: center;
  color: var(--color-muted);
  font-size: 0.9rem;
  white-space: nowrap;
}

.chart-wrap {
  flex: 1;
  overflow: auto;
  position: relative;
  background: var(--color-surface);
  border-radius: 12px;
  border: 1px solid var(--color-border);
  padding: 0.75rem 0.5rem;
}

#stats-chart .bar {
  fill: var(--color-primary);
  pointer-events: none;
}

#stats-chart .bar-hit {
  cursor: pointer;
}

#stats-chart text {
  fill: var(--color-muted);
}

.chart-tooltip {
  position: absolute;
  z-index: 5;
  max-width: 220px;
  background: var(--color-text);
  color: #fff;
  padding: 0.5rem 0.65rem;
  border-radius: 8px;
  font-size: 0.78rem;
  line-height: 1.4;
  pointer-events: none;
  box-shadow: 0 4px 10px rgba(0, 0, 0, 0.25);
}

.chart-tooltip.hidden {
  display: none;
}

.chart-tooltip .tooltip-total {
  margin: 0.15rem 0 0.3rem;
  opacity: 0.85;
}

.table-wrap {
  flex: 1;
  overflow: auto;
  background: var(--color-surface);
  border-radius: 12px;
  border: 1px solid var(--color-border);
}

table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.85rem;
}

thead th {
  position: sticky;
  top: 0;
  background: var(--color-surface);
  border-bottom: 1px solid var(--color-border);
  padding: 0.55rem 0.5rem;
  text-align: left;
  white-space: nowrap;
  cursor: pointer;
  user-select: none;
}

thead th::after {
  content: "";
  margin-left: 0.25rem;
}

thead th.sort-asc::after {
  content: "▲";
  font-size: 0.65em;
}

thead th.sort-desc::after {
  content: "▼";
  font-size: 0.65em;
}

tbody td {
  padding: 0.5rem;
  border-bottom: 1px solid var(--color-border);
  white-space: nowrap;
}

tbody tr:last-child td {
  border-bottom: none;
}

#foods-table tbody tr {
  cursor: pointer;
}

#foods-table tbody tr:active {
  background: #f0ecf7;
}

.row-actions {
  display: flex;
  gap: 0.4rem;
}

.row-actions button {
  border: none;
  background: none;
  font-size: 1.1rem;
  cursor: pointer;
  padding: 0.15rem 0.3rem;
}

.status {
  margin: 0.5rem 0 0;
  color: var(--color-muted);
  font-size: 0.8rem;
}

.tab-bar {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  display: flex;
  background: var(--color-surface);
  border-top: 1px solid var(--color-border);
  padding-bottom: var(--safe-bottom);
}

.tab-btn {
  flex: 1;
  border: none;
  background: none;
  padding: 0.85rem 0.5rem;
  font-size: 0.95rem;
  color: var(--color-muted);
}

.tab-btn.active {
  color: var(--color-primary);
  font-weight: 600;
}

.modal {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.4);
  display: flex;
  align-items: flex-end;
  justify-content: center;
  z-index: 10;
}

.modal.hidden {
  display: none;
}

.modal-content {
  background: var(--color-surface);
  width: 100%;
  max-width: 420px;
  border-radius: 16px 16px 0 0;
  padding: 1.25rem;
  padding-bottom: calc(1.25rem + var(--safe-bottom));
}

.modal-content h2 {
  margin: 0 0 0.25rem;
  font-size: 1.1rem;
}

.modal-food-name {
  color: var(--color-muted);
  margin: 0 0 1rem;
}

.modal-content label {
  display: block;
  margin-bottom: 0.9rem;
  font-size: 0.9rem;
  color: var(--color-muted);
}

.modal-content input {
  display: block;
  width: 100%;
  margin-top: 0.3rem;
  padding: 0.55rem 0.6rem;
  border: 1px solid var(--color-border);
  border-radius: 8px;
  font-size: 1rem;
}

.modal-actions {
  display: flex;
  gap: 0.6rem;
  margin-top: 0.5rem;
}

.btn {
  flex: 1;
  padding: 0.7rem;
  border: none;
  border-radius: 10px;
  font-size: 1rem;
  cursor: pointer;
}

.btn.primary {
  background: var(--color-primary);
  color: #fff;
}

.btn.primary:active {
  background: var(--color-primary-dark);
}

.btn.secondary {
  background: #eee8f4;
  color: var(--color-text);
}
