:root {
  --bg: #F3F6F5;
  --surface: #FFFFFF;
  --surface-subtle: #EDF2F0;
  --border: #DCE5E2;
  --text: #162421;
  --text-muted: #5A6E69;
  --primary: #2A7B6F;
  --primary-hover: #21645A;
  --primary-soft: #E3F2EF;
  --danger: #C94040;
  --danger-soft: #FCEAEA;

  /* Bristol category colors */
  --bristol-constip-bg: #FEF3E2;
  --bristol-constip-fg: #9A5B13;
  --bristol-ideal-bg: #E4F5EC;
  --bristol-ideal-fg: #1F6E48;
  --bristol-loose-bg: #FCECE6;
  --bristol-loose-fg: #B34320;
  --bristol-liquid-bg: #FCE6E8;
  --bristol-liquid-fg: #A82436;

  --radius-lg: 16px;
  --radius-md: 12px;
  --radius-sm: 9px;
  --shadow-sm: 0 1px 2px rgba(18, 36, 33, 0.04);
  --tab-height: 64px;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0E1413;
    --surface: #17211F;
    --surface-subtle: #1F2D2A;
    --border: #293A36;
    --text: #ECF3F1;
    --text-muted: #93ABA5;
    --primary: #3FA393;
    --primary-hover: #4DB8A7;
    --primary-soft: #1B3833;
    --danger: #E56B6B;
    --danger-soft: #361C1E;

    --bristol-constip-bg: #362814;
    --bristol-constip-fg: #F5BC6B;
    --bristol-ideal-bg: #163326;
    --bristol-ideal-fg: #6AD49B;
    --bristol-loose-bg: #382118;
    --bristol-loose-fg: #F49773;
    --bristol-liquid-bg: #3A1B21;
    --bristol-liquid-fg: #F58492;

    --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.25);
  }
}

* {
  box-sizing: border-box;
  -webkit-tap-highlight-color: transparent;
}

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  font-size: 16px;
  line-height: 1.4;
  -webkit-font-smoothing: antialiased;
}

.hidden {
  display: none !important;
}

.app-shell {
  max-width: 540px;
  margin: 0 auto;
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
}

/* Header */
.app-header {
  position: sticky;
  top: 0;
  z-index: 20;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: calc(12px + env(safe-area-inset-top)) 16px 12px;
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: blur(14px);
  -webkit-backdrop-filter: blur(14px);
  border-bottom: 1px solid var(--border);
}

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

.brand-logo {
  width: 38px;
  height: 38px;
  flex-shrink: 0;
  border-radius: 10px;
  overflow: hidden;
  box-shadow: 0 2px 6px rgba(30, 92, 84, 0.24);
  display: flex;
  align-items: center;
  justify-content: center;
}

.brand-logo img {
  width: 100%;
  height: 100%;
  display: block;
}

.brand-text {
  display: flex;
  flex-direction: column;
  line-height: 1.1;
}

.app-title {
  margin: 0;
  font-size: 18px;
  font-weight: 700;
  letter-spacing: -0.02em;
}

.app-subtitle {
  font-size: 11px;
  font-weight: 500;
  color: var(--text-muted);
  margin-top: 1px;
}

.privacy-pill {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  padding: 5px 10px;
  border-radius: 999px;
  background: var(--primary-soft);
  color: var(--primary);
  font-size: 12px;
  font-weight: 600;
}

/* Main Content */
.app-main {
  flex: 1;
  padding: 14px 14px calc(var(--tab-height) + env(safe-area-inset-bottom) + 24px);
}

.view {
  display: none;
  animation: fadeIn 0.16s ease-out;
}

.view.active {
  display: block;
}

@keyframes fadeIn {
  from { opacity: 0; transform: translateY(3px); }
  to { opacity: 1; transform: translateY(0); }
}

/* Edit Banner */
.edit-banner {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 14px;
  margin-bottom: 12px;
  border-radius: var(--radius-md);
  background: var(--primary-soft);
  color: var(--primary);
  font-size: 14px;
  font-weight: 600;
}

/* Cards */
.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 14px;
  margin-bottom: 12px;
  box-shadow: var(--shadow-sm);
}

.compact-card {
  padding: 12px 14px;
}

.card-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 10px;
}

.field-label {
  font-size: 14px;
  font-weight: 600;
  color: var(--text);
}

.required-dot {
  color: var(--primary);
}

.field-hint {
  font-size: 13px;
  color: var(--text-muted);
  font-weight: 500;
}

.optional-tag {
  font-size: 12px;
  font-weight: 500;
  color: var(--text-muted);
  margin-left: 4px;
}

.row-between {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
}

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

.timestamp-value {
  font-size: 15px;
  font-weight: 600;
  color: var(--primary);
}

.time-editor {
  margin-top: 12px;
  padding-top: 12px;
  border-top: 1px solid var(--border);
  display: flex;
  align-items: center;
  gap: 10px;
}

.datetime-input {
  flex: 1;
  min-width: 0;
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--surface-subtle);
  color: var(--text);
  font-family: inherit;
  font-size: 15px;
}

.pill-btn {
  border: 1px solid var(--border);
  background: var(--surface-subtle);
  color: var(--text);
  font-family: inherit;
  font-size: 13px;
  font-weight: 600;
  padding: 7px 12px;
  border-radius: 999px;
  cursor: pointer;
}

.text-btn {
  border: none;
  background: transparent;
  color: var(--primary);
  font-family: inherit;
  font-size: 13px;
  font-weight: 600;
  padding: 6px 8px;
  cursor: pointer;
  white-space: nowrap;
}

/* Bristol Stool Scale Grid */
.bristol-grid {
  display: grid;
  grid-template-columns: 1fr;
  gap: 7px;
}

.bristol-option {
  display: flex;
  align-items: center;
  gap: 12px;
  width: 100%;
  padding: 9px 11px;
  border: 1.5px solid var(--border);
  border-radius: var(--radius-md);
  background: var(--surface);
  color: var(--text);
  font-family: inherit;
  text-align: left;
  cursor: pointer;
  transition: border-color 0.12s, background-color 0.12s, transform 0.08s;
}

.bristol-option:active {
  transform: scale(0.99);
}

.bristol-option.selected {
  border-color: var(--primary);
  background: var(--primary-soft);
}

.bristol-num {
  width: 32px;
  height: 32px;
  flex-shrink: 0;
  border-radius: 9px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 700;
  font-size: 15px;
}

.bristol-cat-constip {
  background: var(--bristol-constip-bg);
  color: var(--bristol-constip-fg);
}
.bristol-cat-ideal {
  background: var(--bristol-ideal-bg);
  color: var(--bristol-ideal-fg);
}
.bristol-cat-loose {
  background: var(--bristol-loose-bg);
  color: var(--bristol-loose-fg);
}
.bristol-cat-liquid {
  background: var(--bristol-liquid-bg);
  color: var(--bristol-liquid-fg);
}

.bristol-icon {
  width: 34px;
  height: 24px;
  flex-shrink: 0;
  color: var(--text-muted);
}

.bristol-option.selected .bristol-icon {
  color: var(--primary);
}

.bristol-text {
  display: flex;
  flex-direction: column;
  min-width: 0;
}

.bristol-name {
  font-size: 14px;
  font-weight: 600;
  color: var(--text);
}

.bristol-desc {
  font-size: 12px;
  color: var(--text-muted);
}

/* Segmented Controls */
.segmented {
  display: flex;
  gap: 6px;
  background: var(--surface-subtle);
  padding: 4px;
  border-radius: var(--radius-md);
}

.segmented-sm {
  padding: 3px;
}

.seg-btn {
  flex: 1;
  border: none;
  background: transparent;
  color: var(--text-muted);
  font-family: inherit;
  font-size: 14px;
  font-weight: 600;
  padding: 10px 6px;
  border-radius: var(--radius-sm);
  cursor: pointer;
  transition: background-color 0.14s, color 0.14s, box-shadow 0.14s;
}

.segmented-sm .seg-btn {
  padding: 7px 6px;
  font-size: 13px;
}

.seg-btn.active {
  background: var(--surface);
  color: var(--primary);
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.08);
}

.sub-field {
  margin-top: 12px;
  padding-top: 12px;
  border-top: 1px dashed var(--border);
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.sub-label {
  font-size: 13px;
  font-weight: 600;
  color: var(--text-muted);
}

.divider {
  height: 1px;
  background: var(--border);
  margin: 14px 0;
}

/* Notes */
.notes-input {
  width: 100%;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--surface-subtle);
  color: var(--text);
  font-family: inherit;
  font-size: 15px;
  padding: 10px 12px;
  resize: vertical;
}

.notes-input:focus {
  outline: none;
  border-color: var(--primary);
}

/* Buttons */
.submit-wrap {
  margin-top: 6px;
}

.primary-btn {
  width: 100%;
  border: none;
  border-radius: var(--radius-md);
  background: var(--primary);
  color: #FFFFFF;
  font-family: inherit;
  font-size: 16px;
  font-weight: 700;
  padding: 15px 18px;
  cursor: pointer;
  transition: background-color 0.14s, transform 0.08s;
}

.primary-btn:hover {
  background: var(--primary-hover);
}

.primary-btn:active {
  transform: scale(0.99);
}

.secondary-btn {
  display: block;
  width: 100%;
  text-align: center;
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  background: var(--surface-subtle);
  color: var(--text);
  font-family: inherit;
  font-size: 15px;
  font-weight: 600;
  padding: 13px 16px;
  cursor: pointer;
}

.danger-btn {
  width: 100%;
  margin-top: 10px;
  border: 1px solid transparent;
  border-radius: var(--radius-md);
  background: var(--danger-soft);
  color: var(--danger);
  font-family: inherit;
  font-size: 14px;
  font-weight: 600;
  padding: 12px 16px;
  cursor: pointer;
}

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

/* Stats Strip (History) */
.stats-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
  margin-bottom: 14px;
}

.stat-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  padding: 12px 10px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
}

.stat-value {
  font-size: 22px;
  font-weight: 700;
  color: var(--primary);
  letter-spacing: -0.02em;
}

.stat-label {
  font-size: 12px;
  color: var(--text-muted);
  font-weight: 500;
}

/* History List */
.day-group {
  margin-bottom: 16px;
}

.day-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  font-size: 13px;
  font-weight: 700;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.04em;
  padding: 4px 4px 8px;
}

.entry-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 12px 14px;
  margin-bottom: 8px;
  box-shadow: var(--shadow-sm);
}

.entry-top {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  margin-bottom: 8px;
}

.entry-left {
  display: flex;
  align-items: center;
  gap: 10px;
}

.entry-bristol-badge {
  padding: 4px 10px;
  border-radius: 999px;
  font-size: 13px;
  font-weight: 700;
}

.entry-time {
  font-size: 14px;
  font-weight: 600;
  color: var(--text);
}

.entry-actions {
  display: flex;
  gap: 4px;
}

.icon-action-btn {
  border: none;
  background: transparent;
  color: var(--text-muted);
  font-size: 13px;
  font-weight: 600;
  padding: 4px 8px;
  border-radius: 6px;
  cursor: pointer;
}

.icon-action-btn.delete-action {
  color: var(--danger);
}

.entry-tags {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}

.tag {
  font-size: 12px;
  font-weight: 600;
  padding: 4px 8px;
  border-radius: 7px;
  background: var(--surface-subtle);
  color: var(--text-muted);
}

.tag-warn {
  background: var(--bristol-loose-bg);
  color: var(--bristol-loose-fg);
}

.tag-high {
  background: var(--bristol-liquid-bg);
  color: var(--bristol-liquid-fg);
}

.entry-note {
  margin-top: 8px;
  padding-top: 8px;
  border-top: 1px solid var(--border);
  font-size: 13px;
  color: var(--text-muted);
  white-space: pre-wrap;
}

.empty-state {
  text-align: center;
  padding: 44px 20px;
  color: var(--text-muted);
}

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

.empty-state p {
  margin: 0;
  font-size: 14px;
}

/* Data / Export View */
.section-title {
  margin: 0 0 6px;
  font-size: 16px;
  font-weight: 700;
}

.section-desc {
  margin: 0 0 14px;
  font-size: 14px;
  color: var(--text-muted);
}

.small-note {
  margin-top: 10px;
  margin-bottom: 4px;
  font-size: 13px;
}

.info-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 8px 0;
  border-bottom: 1px solid var(--border);
  font-size: 14px;
}

/* Bottom Tab Bar */
.tab-bar {
  position: fixed;
  bottom: 0;
  left: 50%;
  transform: translateX(-50%);
  width: 100%;
  max-width: 540px;
  display: flex;
  background: color-mix(in srgb, var(--surface) 92%, transparent);
  backdrop-filter: blur(14px);
  -webkit-backdrop-filter: blur(14px);
  border-top: 1px solid var(--border);
  padding: 6px 8px calc(8px + env(safe-area-inset-bottom));
  z-index: 30;
}

.tab-btn {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 3px;
  border: none;
  background: transparent;
  color: var(--text-muted);
  font-family: inherit;
  font-size: 11px;
  font-weight: 600;
  padding: 4px 0;
  cursor: pointer;
}

.tab-btn.active {
  color: var(--primary);
}

/* Toast */
.toast {
  position: fixed;
  left: 50%;
  bottom: calc(var(--tab-height) + env(safe-area-inset-bottom) + 14px);
  transform: translateX(-50%) translateY(16px);
  background: #162421;
  color: #FFFFFF;
  padding: 10px 18px;
  border-radius: 999px;
  font-size: 14px;
  font-weight: 600;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s, transform 0.2s;
  z-index: 50;
  white-space: nowrap;
  box-shadow: 0 6px 20px rgba(0, 0, 0, 0.2);
}

.toast.show {
  opacity: 1;
  transform: translateX(-50%) translateY(0);
}
