:root {
  --bg:        #FAFAF7;
  --surface:   #FFFFFF;
  --surface-2: #F2F0EB;
  --ink:       #15151A;
  --muted:     #6E6E72;
  --muted-2:   #A0A09A;
  --line:      #E5E2DA;
  --accent:    #8B2D2D;     /* deep theater red */
  --accent-2:  #FBEFEF;
  --gold:      #B8923F;
  --ok:        #2E7D32;
  --sans:      ui-sans-serif, system-ui, -apple-system, "Segoe UI", Inter, "Helvetica Neue", sans-serif;
  --mono:      ui-monospace, "SF Mono", Menlo, monospace;
  --shadow:    0 1px 2px rgba(20,20,25,0.04), 0 6px 24px rgba(20,20,25,0.06);
}

* { box-sizing: border-box; margin: 0; padding: 0; -webkit-tap-highlight-color: transparent; }
html { -webkit-text-size-adjust: 100%; height: 100%; }
body {
  background: var(--bg);
  color: var(--ink);
  font-family: var(--sans);
  font-size: 16px;
  line-height: 1.55;
  -webkit-font-smoothing: antialiased;
  min-height: 100svh;
  overflow-x: hidden;
}
button { font-family: inherit; cursor: pointer; }
input, textarea, select { font-family: inherit; }

/* ── App header ── */
.app-header {
  position: sticky; top: 0; z-index: 30;
  padding: 14px max(18px, env(safe-area-inset-left)) 14px max(18px, env(safe-area-inset-right));
  background: rgba(250,250,247,0.92);
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
  border-bottom: 1px solid var(--line);
  display: flex; align-items: center; justify-content: space-between;
}
.brand { display: flex; align-items: center; gap: 10px; }
.brand-mark { width: 28px; height: 28px; background: var(--ink); color: var(--gold); border-radius: 7px; display: grid; place-items: center; font-family: Georgia, serif; font-weight: 700; font-size: 18px; }
.brand-text { font-weight: 600; font-size: 15px; letter-spacing: -0.005em; }
.brand-sub { font-size: 12px; color: var(--muted); margin-left: 6px; }
.header-actions { display: flex; gap: 8px; }

/* ── Auth gate ── */
.auth-wrap {
  min-height: 100svh;
  display: grid; place-items: center;
  padding: 32px 22px;
  background: var(--ink);
  color: #fff;
}
.auth-card {
  background: var(--ink);
  border: 1px solid #2a2a2f;
  border-radius: 14px;
  padding: 32px;
  width: 100%; max-width: 380px;
  text-align: center;
}
.auth-mark { width: 56px; height: 56px; margin: 0 auto 18px; background: #25252a; color: var(--gold); border-radius: 12px; display: grid; place-items: center; font-family: Georgia, serif; font-weight: 700; font-size: 30px; }
.auth-title { font-size: 18px; font-weight: 600; margin-bottom: 6px; }
.auth-sub { font-size: 13px; color: #B0B0B5; margin-bottom: 22px; }
.auth-form { display: flex; flex-direction: column; gap: 10px; }
.auth-input {
  background: #1f1f24; border: 1px solid #2c2c33; color: #fff;
  padding: 13px 14px; border-radius: 10px; font-size: 16px; outline: none;
  width: 100%;
}
.auth-input:focus { border-color: var(--gold); }
.auth-btn {
  background: var(--gold); color: var(--ink);
  border: 0; padding: 13px 14px; border-radius: 10px;
  font-size: 15px; font-weight: 600;
}
.auth-btn:disabled { opacity: 0.5; }
.auth-err { color: #FFB4B4; font-size: 13px; min-height: 18px; }

/* ── Main view ── */
.main {
  padding: 18px max(18px, env(safe-area-inset-left)) 100px max(18px, env(safe-area-inset-right));
  max-width: 1280px; margin: 0 auto;
}
.group {
  margin-top: 26px;
}
.group:first-of-type { margin-top: 6px; }
.group-head {
  display: flex; align-items: baseline; justify-content: space-between;
  margin-bottom: 12px; padding: 0 2px;
}
.group-title {
  font-family: Georgia, "Iowan Old Style", serif;
  font-size: 18px; font-weight: 700;
  letter-spacing: -0.005em;
  color: var(--ink);
}
.group-count { font-family: var(--mono); font-size: 11px; color: var(--muted); letter-spacing: 0.06em; }

.grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(132px, 1fr));
  gap: 12px;
}
.card {
  position: relative;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 12px;
  overflow: hidden;
  cursor: pointer;
  transition: transform 0.12s ease, box-shadow 0.18s ease, border-color 0.18s ease;
  -webkit-user-select: none; user-select: none;
}
.card:active { transform: scale(0.98); }
.card:hover { box-shadow: var(--shadow); border-color: var(--muted-2); }
.card-photo {
  aspect-ratio: 1 / 1;
  width: 100%;
  object-fit: cover;
  background: var(--surface-2);
  display: block;
}
.card-photo-placeholder {
  aspect-ratio: 1 / 1;
  display: grid; place-items: center;
  background: linear-gradient(135deg, #2a2a30, #15151a);
  color: var(--gold);
  font-family: Georgia, serif; font-size: 36px; font-weight: 700;
}
.card-body { padding: 10px 12px 12px; }
.card-name {
  font-size: 14px;
  font-weight: 600;
  letter-spacing: -0.005em;
  line-height: 1.25;
  margin-bottom: 2px;
  word-break: break-word;
}
.card-role { font-size: 12px; color: var(--muted); line-height: 1.3; min-height: 16px; }
.card-badge {
  position: absolute; top: 8px; right: 8px;
  background: rgba(20,20,25,0.85);
  color: #fff;
  font-family: var(--mono); font-size: 10px;
  padding: 3px 8px; border-radius: 100px;
  letter-spacing: 0.04em;
  min-width: 22px; text-align: center;
}
.card-badge.zero { background: rgba(255,255,255,0.85); color: var(--muted); border: 1px solid var(--line); }

/* "Add subject" tile */
.card-add {
  background: transparent;
  border: 1.5px dashed var(--muted-2);
  border-radius: 12px;
  display: grid; place-items: center;
  aspect-ratio: 1 / 1.18;
  color: var(--muted);
  font-size: 13px;
  transition: border-color 0.18s ease, color 0.18s ease, background 0.18s ease;
}
.card-add:hover { color: var(--accent); border-color: var(--accent); background: var(--accent-2); }
.card-add-plus { font-size: 28px; line-height: 1; margin-bottom: 6px; }

/* ── Buttons ── */
.btn {
  background: var(--ink); color: #fff;
  border: 0;
  padding: 10px 14px;
  border-radius: 10px;
  font-size: 14px; font-weight: 500;
  letter-spacing: -0.005em;
}
.btn-ghost { background: transparent; color: var(--muted); border: 1px solid var(--line); }
.btn-ghost:hover { background: var(--surface-2); color: var(--ink); }
.btn-accent { background: var(--accent); color: #fff; }
.btn-danger { background: transparent; color: #c92a2a; border: 1px solid #f5d5d5; }
.btn-danger:hover { background: #fff5f5; }
.btn-small { padding: 6px 10px; font-size: 12.5px; border-radius: 7px; }

/* ── Modal ── */
.modal-scrim {
  position: fixed; inset: 0; z-index: 100;
  background: rgba(20,20,25,0.55);
  display: grid; place-items: center;
  padding: 20px max(14px, env(safe-area-inset-left)) max(20px, env(safe-area-inset-bottom)) max(14px, env(safe-area-inset-right));
  animation: scrim-in 0.18s ease;
}
@keyframes scrim-in { from { opacity: 0; } to { opacity: 1; } }
.modal {
  background: var(--surface);
  border-radius: 14px;
  width: 100%; max-width: 560px;
  max-height: calc(100svh - 40px);
  overflow: hidden;
  display: flex; flex-direction: column;
  box-shadow: 0 12px 60px rgba(0,0,0,0.32);
  animation: modal-in 0.22s cubic-bezier(0.22, 1, 0.36, 1);
}
@keyframes modal-in { from { transform: translateY(14px); opacity: 0; } to { transform: none; opacity: 1; } }
.modal-head {
  display: flex; align-items: center; gap: 14px;
  padding: 16px 18px;
  border-bottom: 1px solid var(--line);
}
.modal-photo {
  width: 56px; height: 56px;
  border-radius: 50%;
  object-fit: cover;
  background: var(--surface-2);
}
.modal-photo-placeholder {
  width: 56px; height: 56px;
  border-radius: 50%;
  display: grid; place-items: center;
  background: linear-gradient(135deg, #2a2a30, #15151a);
  color: var(--gold);
  font-family: Georgia, serif; font-size: 22px; font-weight: 700;
}
.modal-titles { flex: 1; min-width: 0; }
.modal-name { font-size: 18px; font-weight: 600; letter-spacing: -0.01em; word-break: break-word; }
.modal-role { font-size: 13px; color: var(--muted); }
.modal-close {
  background: transparent; border: 0; color: var(--muted);
  font-size: 22px; line-height: 1;
  padding: 6px 10px; border-radius: 8px;
}
.modal-close:hover { background: var(--surface-2); color: var(--ink); }

.modal-body {
  flex: 1; min-height: 0; overflow-y: auto;
  padding: 16px 18px;
}

.facts {
  display: flex; flex-direction: column; gap: 12px;
}
.fact {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 12px 14px;
}
.fact-content {
  font-size: 15px;
  line-height: 1.5;
  white-space: pre-wrap;
  word-wrap: break-word;
}
.fact-meta {
  display: flex; gap: 12px; justify-content: space-between;
  margin-top: 10px;
  font-family: var(--mono); font-size: 11px; color: var(--muted);
  letter-spacing: 0.02em;
}
.fact-actions { display: flex; gap: 6px; }
.fact-edit-area {
  background: var(--surface-2);
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 12px 14px;
  display: flex; flex-direction: column; gap: 10px;
}
.fact-textarea {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 10px 12px;
  font-size: 15px; line-height: 1.5;
  width: 100%; min-height: 90px; resize: vertical;
  outline: none;
}
.fact-textarea:focus { border-color: var(--accent); }
.fact-by-input {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 9px 12px;
  font-size: 13.5px;
  outline: none;
  max-width: 220px;
}

.add-fact-card {
  background: var(--accent-2);
  border: 1px solid #efd3d3;
  border-radius: 10px;
  padding: 14px;
  margin-bottom: 14px;
  display: flex; flex-direction: column; gap: 10px;
}
.add-fact-btn {
  background: transparent;
  border: 1.5px dashed var(--muted-2);
  border-radius: 10px;
  padding: 14px;
  width: 100%;
  font-size: 14px; color: var(--muted);
  margin-bottom: 14px;
  display: flex; align-items: center; justify-content: center; gap: 8px;
  transition: border-color 0.18s ease, color 0.18s ease, background 0.18s ease;
}
.add-fact-btn:hover { border-color: var(--accent); color: var(--accent); background: var(--accent-2); }

.empty {
  text-align: center; color: var(--muted);
  padding: 24px 12px;
  font-size: 14px;
}

/* ── New subject form ── */
.form-row { display: flex; flex-direction: column; gap: 6px; margin-bottom: 14px; }
.form-label { font-size: 13px; color: var(--muted); font-weight: 500; }
.form-input, .form-select {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 11px 12px;
  font-size: 15px;
  outline: none;
}
.form-input:focus, .form-select:focus { border-color: var(--accent); }
.photo-upload {
  display: flex; align-items: center; gap: 12px;
}
.photo-preview {
  width: 64px; height: 64px;
  border-radius: 50%;
  object-fit: cover;
  background: var(--surface-2);
  border: 1px solid var(--line);
}
.photo-preview-placeholder {
  width: 64px; height: 64px;
  border-radius: 50%;
  background: var(--surface-2); border: 1px dashed var(--muted-2);
  display: grid; place-items: center;
  color: var(--muted-2);
  font-size: 11px;
}
.photo-pick {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 9px 14px;
  font-size: 13px;
  color: var(--ink);
}
.photo-pick:hover { background: var(--surface-2); }
.hidden-file { display: none; }

.modal-foot {
  padding: 14px 18px;
  border-top: 1px solid var(--line);
  display: flex; gap: 10px; justify-content: flex-end;
  background: var(--bg);
}

/* ── Toast ── */
.toast {
  position: fixed;
  bottom: max(20px, env(safe-area-inset-bottom));
  left: 50%; transform: translateX(-50%);
  background: var(--ink); color: #fff;
  padding: 10px 18px;
  border-radius: 100px;
  font-size: 14px;
  z-index: 200;
  box-shadow: 0 6px 24px rgba(0,0,0,0.25);
  animation: toast-in 0.22s ease;
}
@keyframes toast-in { from { transform: translate(-50%, 12px); opacity: 0; } to { transform: translateX(-50%); opacity: 1; } }

/* ── Loading ── */
.loading { display: grid; place-items: center; padding: 48px 0; color: var(--muted); font-size: 14px; }

/* ── Responsive ── */
@media (max-width: 480px) {
  .grid { grid-template-columns: repeat(auto-fill, minmax(118px, 1fr)); gap: 10px; }
  .card-name { font-size: 13px; }
  .card-role { font-size: 11.5px; }
  .modal-head { padding: 14px 14px; }
  .modal-body { padding: 14px; }
  .modal-foot { padding: 12px 14px; }
}
