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

:root {
  color-scheme: light;
  --color-primary:      #4a90e2;
  --color-primary-dark: #2e6fbf;
  --color-danger:       #e25454;
  --color-danger-dark:  #bf2e2e;
  --color-danger-bg:    #fdeaea;
  --color-bg:           #f2f2f7;
  --color-surface:      #ffffff;
  --color-border:       #e0e6ed;
  --color-text:         #1a2332;
  --color-muted:        #6b7a8d;
  --radius:             12px;
  --shadow:             0 2px 8px rgba(0,0,0,.08), 0 0 1px rgba(0,0,0,.06);
  --shadow-hover:       0 4px 16px rgba(0,0,0,.12), 0 0 1px rgba(0,0,0,.08);
  --header-shadow:      0 1px 0 rgba(0,0,0,.15);
  --nav-duration:       0.32s;
  --nav-ease:           cubic-bezier(0.25, 0.46, 0.45, 0.94);
}

html, body {
  height: 100%;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  background: var(--color-bg);
  color: var(--color-text);
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
}

/* ── App shell & navigation screens ──────────────────────────────────────── */
/*
 * position:fixed takes the app out of document flow entirely — there is
 * nothing left to scroll at the document level, so iOS bounce never happens
 * on the page. inset:0 + max-width + margin:auto centres it on wide screens.
 */
.app {
  position: fixed;
  inset: 0;
  max-width: 600px;
  margin: 0 auto;
  overflow: hidden;
}

/* Both screens share these base styles */
.screen {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  background: var(--color-bg);
  will-change: transform;
  transition: transform var(--nav-duration) var(--nav-ease);
}

/* Overview slides 28 % to the left while detail is visible (iOS parallax) */
.screen-overview.pushed {
  transform: translateX(-28%);
  pointer-events: none;
}

/* Detail starts off-screen to the right */
.screen-detail {
  transform: translateX(100%);
  box-shadow: -6px 0 24px rgba(0,0,0,.12);
}
.screen-detail.active {
  transform: translateX(0);
}
/* Pushed left when event-detail is open (iOS parallax) */
.screen-detail.pushed {
  transform: translateX(-28%);
  pointer-events: none;
}

/* Event-detail: same slide-in as screen-detail */
.screen-event-detail {
  transform: translateX(100%);
  box-shadow: -6px 0 24px rgba(0,0,0,.12);
}
.screen-event-detail.active {
  transform: translateX(0);
}

/* Fills the .screen-detail flex column (touch handlers live here) */
.detail-wrapper {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-height: 0;
}

/* ── iOS-style navigation header ─────────────────────────────────────────── */
.app-header {
  background: #1c1c1e;
  color: white;
  flex-shrink: 0;
  z-index: 10;
  /* Thin separator line to distinguish header from list content */
  border-bottom: 1px solid rgba(255,255,255,.12);
  /* Respect iPhone notch / Dynamic Island */
  padding-top: env(safe-area-inset-top, 0px);
}

.header-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  height: 44px;
  padding: 0 4px;
}

.header-bar h1 {
  font-size: 1.0625rem;
  font-weight: 600;
  letter-spacing: -.25px;
  flex: 1;
  text-align: center;
  pointer-events: none;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  padding: 0 8px;
}

/* ── Header buttons ───────────────────────────────────────────────────────── */
.header-btn {
  background: transparent;
  border: none;
  color: white;
  font-size: .9375rem;
  cursor: pointer;
  padding: 0 12px;
  height: 44px;
  min-width: 64px;
  display: flex;
  align-items: center;
  white-space: nowrap;
  transition: opacity .15s;
  -webkit-tap-highlight-color: transparent;
}

.header-btn:active { opacity: .6; }

.header-btn-left   { justify-content: flex-start; }
.header-btn-right  { justify-content: flex-end; font-size: 1.5rem; font-weight: 300; }
.header-btn-import { cursor: pointer; font-size: 1.1rem; min-width: 44px; }
.header-btn-back   { justify-content: flex-start; font-size: 1.0625rem; }

.header-right {
  display: flex;
  align-items: center;
}

/* ── Add topic bar ────────────────────────────────────────────────────────── */
.add-topic-bar {
  display: flex;
  gap: 8px;
  padding: 10px 12px 14px;
  border-top: 1px solid rgba(255,255,255,.2);
  animation: slide-down .2s ease;
}

@keyframes slide-down {
  from { opacity: 0; transform: translateY(-6px); }
  to   { opacity: 1; transform: translateY(0); }
}

.add-topic-bar .topic-input {
  background: rgba(255,255,255,.18);
  border-color: rgba(255,255,255,.35);
  color: white;
}
.add-topic-bar .topic-input::placeholder { color: rgba(255,255,255,.65); }
.add-topic-bar .topic-input:focus {
  border-color: rgba(255,255,255,.7);
  background: rgba(255,255,255,.25);
}

/* ── Shared input styles ──────────────────────────────────────────────────── */
.topic-input {
  flex: 1;
  height: 40px;
  padding: 0 12px;
  border: 2px solid var(--color-border);
  border-radius: var(--radius);
  font-size: 1rem;
  background: var(--color-surface);
  color: var(--color-text);
  transition: border-color .15s, background .15s;
  outline: none;
}
.topic-input:focus { border-color: var(--color-primary); }
.topic-input::placeholder { color: var(--color-muted); }

/* ── Buttons ──────────────────────────────────────────────────────────────── */
.btn {
  height: 40px;
  padding: 0 16px;
  border: none;
  border-radius: var(--radius);
  font-size: .9rem;
  font-weight: 600;
  cursor: pointer;
  white-space: nowrap;
  transition: background .15s, opacity .15s;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 4px;
  -webkit-tap-highlight-color: transparent;
}
.btn:active { opacity: .75; }
.btn-add {
  background: rgba(255,255,255,.25);
  color: white;
  min-width: 72px;
  border: 1.5px solid rgba(255,255,255,.45);
}
.btn-add:hover { background: rgba(255,255,255,.35); }

/* ── Main content area ────────────────────────────────────────────────────── */
.app-main {
  flex: 1;
  overflow-y: auto;
  padding: 16px 16px 32px;
  display: flex;
  flex-direction: column;
  gap: 16px;
  padding-bottom: max(32px, env(safe-area-inset-bottom, 32px));
  /* Momentum scrolling on iOS */
  -webkit-overflow-scrolling: touch;
  /* Contain rubber-banding to this element, not the whole page */
  overscroll-behavior: contain;
}

/* Detail main has no side gap so the card spans edge-to-edge in narrow views */
.app-main--detail {
  padding-left: 0;
  padding-right: 0;
  gap: 0;
}

/* ── Overview: topic list & rows ──────────────────────────────────────────── */
.topic-list {
  display: flex;
  flex-direction: column;
  gap: 1px;
  background: var(--color-border);
  border-radius: var(--radius);
  overflow: hidden;
  box-shadow: var(--shadow);
}

.topic-row {
  display: flex;
  align-items: center;
  background: var(--color-surface);
  padding: 0 4px 0 12px;
  min-height: 64px;
  gap: 8px;
}

/* Tappable area — tracks an event on press */
.topic-row-main {
  flex: 1;
  min-width: 0;
  padding: 12px 0;
  cursor: pointer;
  user-select: none;
  -webkit-tap-highlight-color: transparent;
}
.topic-row-main:active { opacity: .55; }

.topic-row-name {
  display: block;
  font-size: 1rem;
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.topic-row-counts {
  display: block;
  font-size: .75rem;
  color: var(--color-muted);
  margin-top: 2px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  font-variant-numeric: tabular-nums;
}

/* iOS-style disclosure chevron */
.btn-detail {
  background: transparent;
  border: none;
  color: var(--color-muted);
  font-size: 1.25rem;
  line-height: 1;
  cursor: pointer;
  padding: 10px 8px 10px 4px;
  flex-shrink: 0;
  display: flex;
  align-items: center;
  -webkit-tap-highlight-color: transparent;
  transition: color .15s;
}
.btn-detail:active { opacity: .6; }

/* Red delete circle — only shown in edit mode */
.btn-delete-topic {
  width: 28px;
  height: 28px;
  border-radius: 50%;
  background: var(--color-danger);
  color: white;
  border: none;
  font-size: 1.25rem;
  font-weight: 300;
  line-height: 1;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  transition: transform .1s;
  animation: pop-in .18s ease;
  -webkit-tap-highlight-color: transparent;
}
.btn-delete-topic:active { transform: scale(.88); }

@keyframes pop-in {
  from { transform: scale(0); }
  to   { transform: scale(1); }
}

/* ── Detail: event list ───────────────────────────────────────────────────── */
.event-card {
  background: var(--color-surface);
  box-shadow: var(--shadow);
}

/* On larger screens match the side padding of the overview list */
@media (min-width: 481px) {
  .event-card {
    margin: 16px;
    border-radius: var(--radius);
    overflow: hidden;
  }
}

.event-list {
  list-style: none;
}

.event-item {
  position: relative;
  overflow: hidden;
  border-bottom: 1px solid var(--color-border);
}
.event-item:last-child { border-bottom: none; }

/* Slides left to reveal the delete button on swipe */
.event-item-content {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 12px 16px;
  font-size: .875rem;
  color: var(--color-text);
  font-variant-numeric: tabular-nums;
  background: var(--color-surface);
  transition: transform .25s ease;
}
.event-item.swiped .event-item-content {
  transform: translateX(-80px);
}

.event-time { flex: 1; }
.event-icon { font-size: .85rem; opacity: .6; flex-shrink: 0; }

/* Delete button revealed by swipe (or hover on pointer devices) */
.btn-delete-swipe {
  position: absolute;
  right: 0;
  top: 0;
  bottom: 0;
  width: 80px;
  background: var(--color-danger);
  color: white;
  border: none;
  font-size: .875rem;
  font-weight: 600;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transform: translateX(100%);
  transition: transform .25s ease;
  -webkit-tap-highlight-color: transparent;
}
.event-item.swiped .btn-delete-swipe {
  transform: translateX(0);
}
@media (hover: hover) {
  .event-item:hover .btn-delete-swipe { transform: translateX(0); }
}

.event-empty {
  padding: 24px 16px;
  text-align: center;
  color: var(--color-muted);
  font-size: .875rem;
  font-style: italic;
}

/* ── Empty state ──────────────────────────────────────────────────────────── */
.empty-state {
  text-align: center;
  padding: 64px 16px;
  color: var(--color-muted);
}
.empty-state p:first-child {
  font-size: 1.05rem;
  font-weight: 600;
  color: var(--color-text);
  margin-bottom: 6px;
}

/* ── Manual-event modal (bottom sheet) ───────────────────────────────────── */
/*
 * position:absolute so it is contained within .screen-detail (which is
 * position:absolute), avoiding the fixed-inside-transform trap while still
 * covering the whole detail screen including its header.
 */
.modal-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(0,0,0,.45);
  display: flex;
  align-items: flex-end;
  justify-content: center;
  z-index: 100;
  animation: fade-in .18s ease;
}

@keyframes fade-in {
  from { opacity: 0; }
  to   { opacity: 1; }
}

.modal-sheet {
  background: var(--color-surface);
  border-radius: var(--radius) var(--radius) 0 0;
  padding: 24px 20px max(20px, env(safe-area-inset-bottom, 20px));
  width: 100%;
  max-width: 600px;
  display: flex;
  flex-direction: column;
  gap: 16px;
  animation: slide-up .22s var(--nav-ease);
}

@keyframes slide-up {
  from { transform: translateY(100%); }
  to   { transform: translateY(0); }
}

.modal-title {
  font-weight: 600;
  font-size: 1rem;
  color: var(--color-text);
  text-align: center;
}

.modal-datetime-input {
  width: 100%;
  height: 44px;
  padding: 0 12px;
  border: 2px solid var(--color-border);
  border-radius: var(--radius);
  font-size: 1rem;
  background: var(--color-surface);
  color: var(--color-text);
  outline: none;
  font-family: inherit;
  transition: border-color .15s;
}
.modal-datetime-input:focus { border-color: var(--color-primary); }

.modal-actions {
  display: flex;
  gap: 10px;
}

.modal-btn {
  flex: 1;
  height: 44px;
  border: none;
  border-radius: var(--radius);
  font-size: .9375rem;
  font-weight: 600;
  cursor: pointer;
  transition: opacity .15s;
  -webkit-tap-highlight-color: transparent;
}
.modal-btn:active { opacity: .7; }

.modal-btn-cancel {
  background: var(--color-border);
  color: var(--color-text);
}

.modal-btn-add {
  background: var(--color-primary);
  color: white;
}

/* ── Scrollbar ────────────────────────────────────────────────────────────── */
.app-main::-webkit-scrollbar        { width: 4px; }
.app-main::-webkit-scrollbar-track  { background: transparent; }
.app-main::-webkit-scrollbar-thumb  { background: var(--color-border); border-radius: 2px; }

/* ── Mobile tweaks ────────────────────────────────────────────────────────── */
@media (max-width: 480px) {
  .topic-list  { border-radius: 0; box-shadow: none; }
  .app-main    { padding-left: 0; padding-right: 0; }
}

/* ── Colour schemes ───────────────────────────────────────────────────────── */
@media (prefers-color-scheme: dark) {
  :root {
    color-scheme: dark;
    --color-primary:      #5b9fe8;
    --color-primary-dark: #4a90e2;
    --color-danger-bg:    rgba(226,84,84,.15);
    --header-shadow:      0 1px 0 rgba(0,0,0,.40);
    --color-bg:           #000000;
    --color-surface:      #1c1c1e;
    --color-border:       #38383a;
    --color-text:         #e6edf3;
    --color-muted:        #8e8e93;
    --shadow:             0 2px 8px rgba(0,0,0,.40), 0 0 1px rgba(0,0,0,.30);
    --shadow-hover:       0 4px 16px rgba(0,0,0,.55), 0 0 1px rgba(0,0,0,.40);
  }
}

/* ── Load-more button ───────────────────────────────────────────────────────── */
.btn-load-more {
  display: block;
  width: calc(100% - 32px);
  margin: 12px 16px;
  padding: 10px;
  background: var(--color-bg);
  border: 1px solid var(--color-border);
  border-radius: var(--radius);
  color: var(--color-primary);
  font-size: 0.95rem;
  font-weight: 600;
  cursor: pointer;
}
.btn-load-more:active { opacity: 0.7; }

/* ── Loading indicator ──────────────────────────────────────────────────────── */
.loading-indicator {
  padding: 24px;
  text-align: center;
  color: var(--color-muted);
  font-size: 0.95rem;
}

/* Re-invert the header under Smart Invert so it keeps its dark-gray look.
 * filter:invert(1) cancels out the OS-level inversion for this element only. */
@media (inverted-colors: inverted) {
  .app-header { filter: invert(1); }
}

/* light CSS + OS inversion → perceived dark; native UI should follow */
@media (prefers-color-scheme: light) and (inverted-colors: inverted) {
  :root { color-scheme: dark; }
}

/* dark CSS + OS inversion → perceived light; native UI should follow */
@media (prefers-color-scheme: dark) and (inverted-colors: inverted) {
  :root { color-scheme: light; }
}

/* ── Event detail view ──────────────────────────────────────────────────────── */
.event-detail-wrapper {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-height: 0;
}

.event-detail-main {
  flex: 1;
  overflow-y: auto;
  padding: 16px;
  padding-bottom: max(32px, env(safe-area-inset-bottom, 32px));
  -webkit-overflow-scrolling: touch;
  overscroll-behavior: contain;
}

.event-detail-card {
  background: var(--color-surface);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  overflow: hidden;
}

.event-detail-section {
  padding: 4px 0;
}
.event-detail-section + .event-detail-section {
  border-top: 1px solid var(--color-border);
}

.event-detail-row {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  padding: 10px 16px;
  gap: 12px;
}

.event-detail-label {
  font-size: .8125rem;
  color: var(--color-muted);
  flex-shrink: 0;
}

.event-detail-value {
  font-size: .9375rem;
  font-variant-numeric: tabular-nums;
  text-align: right;
  color: var(--color-text);
}

.event-detail-value--muted {
  color: var(--color-muted);
  font-style: italic;
}

/* Make event list rows tappable */
.event-item-content {
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
}
.event-item-content:active { opacity: .6; }
