/* ═══════════════════════════════════════════════
   Clinic Records — Apple-ish design system
   ═══════════════════════════════════════════════ */

:root {
  color-scheme: light;

  /* Palette */
  --bg:           #f5f5f7;
  --surface:      #ffffff;
  --border:       rgba(0, 0, 0, 0.09);
  --border-input: rgba(0, 0, 0, 0.15);
  --border-focus: #0071e3;
  --text:         #1d1d1f;
  --muted-color:  #86868b;
  --accent:       #1d1d1f;
  --blue:         #0071e3;
  --green:        #34c759;
  --red:          #ff3b30;

  /* Shadows */
  --shadow-xs: 0 1px 2px rgba(0,0,0,0.05);
  --shadow-sm: 0 1px 3px rgba(0,0,0,0.07), 0 2px 8px rgba(0,0,0,0.04);
  --shadow-md: 0 4px 16px rgba(0,0,0,0.08), 0 1px 4px rgba(0,0,0,0.05);

  /* Radii */
  --r-xs:  6px;
  --r-sm:  8px;
  --r-md:  10px;
  --r-lg:  14px;
  --r-xl:  18px;

  /* Spacing */
  --gap: 14px;

  /* Font */
  --font: -apple-system, BlinkMacSystemFont, "SF Pro Text", "SF Pro Display",
          "Helvetica Neue", Arial, sans-serif;

  /* Transition */
  --t: 0.14s ease;
}

/* ── Reset ── */
*, *::before, *::after { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font);
  font-size: 15px;
  line-height: 1.55;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

a { color: var(--blue); text-decoration: none; }
a:hover { text-decoration: underline; }

/* ── Typography ── */
h1 { font-size: 26px; font-weight: 700; margin: 0 0 4px; letter-spacing: -0.4px; }
h2 { font-size: 18px; font-weight: 600; margin: 0 0 10px; letter-spacing: -0.2px; }
h3 { font-size: 15px; font-weight: 600; margin: 0 0 8px; }
p  { margin: 0 0 10px; }

.muted { color: var(--muted-color); font-size: 13px; line-height: 1.5; }

/* ── Layout ── */
.container {
  max-width: 980px;
  margin: 0 auto;
  padding: 28px 24px;
}

.row {
  display: flex;
  flex-wrap: wrap;
  gap: var(--gap);
}
.row > * { flex: 1 1 auto; }

.split {
  display: grid;
  grid-template-columns: 1.2fr 1fr;
  gap: var(--gap);
}

/* ── Navigation ── */
.nav {
  position: sticky;
  top: 0;
  z-index: 100;
  background: rgba(255, 255, 255, 0.85);
  backdrop-filter: saturate(180%) blur(20px);
  -webkit-backdrop-filter: saturate(180%) blur(20px);
  border-bottom: 1px solid var(--border);
}

.nav-inner {
  max-width: 980px;
  margin: 0 auto;
  padding: 0 24px;
  height: 52px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

.brand {
  font-size: 15px;
  font-weight: 700;
  color: var(--text);
  text-decoration: none;
  letter-spacing: -0.2px;
  flex-shrink: 0;
}
.brand:hover { text-decoration: none; color: var(--text); }

.brand-accent { color: var(--blue); }

/* ── Card ── */
.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--r-xl);
  box-shadow: var(--shadow-sm);
  padding: 24px;
}
.card + .card { margin-top: var(--gap); }

/* ── Buttons ── */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  border-radius: var(--r-md);
  padding: 7px 14px;
  font-size: 13px;
  font-weight: 500;
  font-family: var(--font);
  border: 1px solid rgba(0, 0, 0, 0.15);
  background: var(--surface);
  color: var(--text);
  text-decoration: none;
  cursor: pointer;
  transition: background var(--t), box-shadow var(--t), transform var(--t), border-color var(--t);
  box-shadow: var(--shadow-xs);
  white-space: nowrap;
  user-select: none;
  -webkit-tap-highlight-color: transparent;
  line-height: 1.4;
}
.btn:hover {
  background: #f0f0f2;
  border-color: rgba(0,0,0,0.2);
  box-shadow: var(--shadow-sm);
  text-decoration: none;
  color: var(--text);
}
.btn:active { transform: scale(0.97); box-shadow: none; }
.btn:focus-visible {
  outline: none;
  box-shadow: 0 0 0 3px rgba(0, 113, 227, 0.35);
}

.btn.primary {
  background: var(--accent);
  color: #ffffff;
  border-color: transparent;
  box-shadow: none;
}
.btn.primary:hover {
  background: #333333;
  border-color: transparent;
  color: #fff;
  box-shadow: none;
}
.btn.primary:active { transform: scale(0.97); }

.btn.danger {
  background: rgba(255, 59, 48, 0.07);
  color: var(--red);
  border-color: rgba(255, 59, 48, 0.2);
  box-shadow: none;
}
.btn.danger:hover {
  background: rgba(255, 59, 48, 0.13);
  border-color: rgba(255, 59, 48, 0.3);
  color: var(--red);
}

/* ── Form inputs — style everything, then reset what shouldn't be styled ── */
input, select, textarea {
  width: 100%;
  padding: 10px 13px;
  border-radius: var(--r-md);
  border: 1px solid var(--border-input);
  background: var(--surface);
  color: var(--text);
  font-family: var(--font);
  font-size: 15px;
  line-height: 1.45;
  outline: none;
  transition: border-color var(--t), box-shadow var(--t);
  -webkit-appearance: none;
  -moz-appearance: none;
  appearance: none;
  box-sizing: border-box;
}
input:focus, select:focus, textarea:focus {
  border-color: var(--border-focus);
  box-shadow: 0 0 0 3px rgba(0, 113, 227, 0.18);
}
textarea { min-height: 100px; resize: vertical; line-height: 1.55; }

/* Select — restore dropdown arrow via SVG background */
select {
  padding-right: 34px;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='8' viewBox='0 0 12 8'%3E%3Cpath d='M1 1l5 5 5-5' stroke='%2386868b' stroke-width='1.5' fill='none' stroke-linecap='round' stroke-linejoin='round'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 12px center;
}

/* Undo styling for button-like inputs */
input[type="submit"],
input[type="button"],
input[type="reset"] {
  width: auto;
  padding: 7px 14px;
  border-radius: var(--r-md);
  cursor: pointer;
}

/* File input */
input[type="file"] {
  padding: 8px 12px;
  background: rgba(0,0,0,0.025);
  font-size: 13px;
  cursor: pointer;
  color: var(--muted-color);
  -webkit-appearance: none;
}
input[type="file"]::file-selector-button {
  border: 1px solid var(--border-input);
  border-radius: var(--r-sm);
  background: var(--surface);
  color: var(--text);
  font-family: var(--font);
  font-size: 13px;
  font-weight: 500;
  padding: 5px 12px;
  margin-right: 12px;
  cursor: pointer;
  transition: background var(--t);
}
input[type="file"]::file-selector-button:hover { background: #f0f0f2; }

/* Checkboxes / radios — restore native appearance */
input[type="checkbox"],
input[type="radio"] {
  width: auto;
  padding: 0;
  margin-right: 6px;
  border: none;
  background: none;
  box-shadow: none;
  accent-color: var(--blue);
  -webkit-appearance: auto;
  -moz-appearance: auto;
  appearance: auto;
}

/* ── Django form.as_p rendering ── */
form p { margin: 0 0 14px; }

form p label {
  display: block;
  font-size: 13px;
  font-weight: 600;
  color: var(--text);
  margin-bottom: 5px;
}
form p input[type="checkbox"] + label,
form p label > input[type="checkbox"] {
  display: inline;
  font-weight: 400;
}
form p .helptext {
  display: block;
  font-size: 12px;
  color: var(--muted-color);
  margin-top: 4px;
}
form ul.errorlist {
  list-style: none;
  margin: 4px 0 0;
  padding: 0;
  color: var(--red);
  font-size: 12px;
}

/* ── Form helpers ── */
.form-control, .form-select {
  width: 100%;
  padding: 9px 12px;
  border-radius: var(--r-md);
  border: 1px solid var(--border-input);
  background: var(--surface);
  font-family: var(--font);
  font-size: 15px;
  outline: none;
  transition: border-color var(--t), box-shadow var(--t);
}
.form-control:focus, .form-select:focus {
  border-color: var(--border-focus);
  box-shadow: 0 0 0 3px rgba(0, 113, 227, 0.18);
}
.form-label {
  display: block;
  margin-bottom: 5px;
  font-size: 13px;
  font-weight: 600;
  color: var(--text);
}
label.muted {
  color: var(--text);
  font-size: 13px;
  font-weight: 600;
  margin-bottom: 5px;
  display: block;
}
.form-text   { display: block; margin-top: 4px; font-size: 12px; }
.text-muted  { color: var(--muted-color); }
.text-danger { color: var(--red); font-size: 12px; margin-top: 4px; }
.mb-3        { margin-bottom: 24px; }
.my-4        { margin-top: 28px; margin-bottom: 28px; }
.d-flex      { display: flex; }
.gap-2       { gap: 8px; }

/* ── Alerts ── */
.alert {
  padding: 12px 16px;
  border-radius: var(--r-md);
  margin-bottom: 16px;
  font-size: 14px;
  line-height: 1.5;
}
.alert-danger {
  background: rgba(255, 59, 48, 0.07);
  color: #c0392b;
  border: 1px solid rgba(255, 59, 48, 0.18);
}
.alert-warning {
  background: rgba(255, 149, 0, 0.07);
  color: #9a5700;
  border: 1px solid rgba(255, 149, 0, 0.2);
}

/* ── Badge ── */
.badge {
  display: inline-block;
  padding: 2px 9px;
  border-radius: 100px;
  font-size: 11px;
  font-weight: 500;
  background: rgba(0,0,0,0.05);
  color: var(--muted-color);
  border: 1px solid rgba(0,0,0,0.07);
  letter-spacing: 0.2px;
  white-space: nowrap;
}

/* ── Table ── */
.table {
  width: 100%;
  border-collapse: collapse;
  font-size: 14px;
}
.table th {
  padding: 10px 14px;
  text-align: left;
  font-size: 11px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  color: var(--muted-color);
  border-bottom: 1px solid var(--border);
  background: transparent;
}
.table td {
  padding: 12px 14px;
  border-bottom: 1px solid rgba(0,0,0,0.04);
  color: var(--text);
  vertical-align: middle;
}
.table tr:last-child td { border-bottom: none; }
.table tbody tr:hover td { background: rgba(0,0,0,0.02); }

/* ── Visit history ── */
.card > .card.visit-row {
  box-shadow: none;
  border-radius: var(--r-lg);
  margin-bottom: 8px;
  background: rgba(0,0,0,0.015);
}
.card > .card.visit-row:last-child { margin-bottom: 0; }

.visit-row {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 16px;
  padding: 16px 18px;
}
.visit-left { flex: 1; min-width: 0; line-height: 1.6; }
.visit-right {
  width: auto;
  flex-shrink: 0;
  display: flex;
  align-items: flex-start;
  padding-top: 2px;
}

/* ── File grid ── */
.file-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(240px, 1fr));
  gap: 14px;
}
.file-card {
  background: rgba(0,0,0,0.02);
  border: 1px solid var(--border);
  border-radius: var(--r-lg);
  padding: 14px;
  display: flex;
  flex-direction: column;
  gap: 10px;
  transition: box-shadow var(--t), background var(--t);
}
.file-card:hover { box-shadow: var(--shadow-sm); background: var(--surface); }
.file-preview {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 110px;
  background: var(--surface);
  border-radius: var(--r-sm);
  overflow: hidden;
  border: 1px solid var(--border);
}
.file-icon      { font-size: 38px; text-align: center; padding: 14px; }
.file-icon.pdf  { color: var(--red); }
.file-icon.doc  { color: var(--blue); }
.file-info      { flex: 1; }
.file-name {
  font-weight: 600;
  font-size: 13px;
  margin-bottom: 4px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  color: var(--text);
}
.file-meta {
  font-size: 11px;
  display: flex;
  flex-wrap: wrap;
  gap: 5px;
  align-items: center;
  color: var(--muted-color);
}
.file-actions { display: flex; gap: 8px; flex-wrap: wrap; }
.file-actions .btn { flex: 1; min-width: 80px; font-size: 12px; padding: 6px 10px; }

/* ── Nested container fix (attachment_upload wraps in .container again) ── */
.container .container { max-width: none; margin: 0; padding: 0; }
.container .container > .card { max-width: 580px; }

/* ── form.as_p extra spacing ── */
form p { margin: 0 0 20px; }

/* ── Button spacing after form fields (only when preceded by as_p <p> fields) ── */
form p ~ .btn,
form p ~ button,
form p ~ .d-flex { margin-top: 20px; }

/* ── Flash messages ── */
.messages-wrap {
  max-width: 980px;
  margin: 0 auto;
  padding: 12px 24px 0;
}
.message {
  padding: 12px 16px;
  border-radius: var(--r-md);
  font-size: 14px;
  font-weight: 500;
  margin-bottom: 8px;
  line-height: 1.5;
}
.message-success {
  background: rgba(52, 199, 89, 0.10);
  color: #1a6b35;
  border: 1px solid rgba(52, 199, 89, 0.28);
}
.message-error,
.message-danger {
  background: rgba(255, 59, 48, 0.07);
  color: #c0392b;
  border: 1px solid rgba(255, 59, 48, 0.18);
}
.message-warning {
  background: rgba(255, 149, 0, 0.07);
  color: #9a5700;
  border: 1px solid rgba(255, 149, 0, 0.2);
}
.message-info {
  background: rgba(0, 113, 227, 0.07);
  color: #004d99;
  border: 1px solid rgba(0, 113, 227, 0.2);
}

/* ── Pagination ── */
.pagination {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
  margin-top: 24px;
  padding-top: 16px;
  border-top: 1px solid var(--border);
}
.pagination .muted { font-size: 13px; }

/* ── Responsive ── */
@media (max-width: 860px) {
  .split { grid-template-columns: 1fr; }
}

@media (max-width: 640px) {
  .container { padding: 16px; }

  .nav-inner {
    flex-wrap: wrap;
    height: auto;
    min-height: 52px;
    gap: 8px;
    padding: 10px 16px;
  }

  .card { padding: 14px; border-radius: var(--r-lg); }
  .row > .card { min-width: calc(50% - 6px); }

  h1 { font-size: 22px; }
  h2 { font-size: 17px; }

  .table {
    display: block;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    white-space: nowrap;
  }

  .visit-row { flex-direction: column; }
  .visit-right { width: 100%; padding-top: 0; }
}

/* ── RTL overrides ─────────────────────────────────────────────────────────── */
[dir="rtl"] body,
[dir="ltr"] body {
  font-family: 'Cairo', -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
}

[dir="rtl"] .nav-inner { flex-direction: row-reverse; }

[dir="rtl"] .table th,
[dir="rtl"] .table td { text-align: right; }

[dir="rtl"] .form-group label { text-align: right; display: block; }

[dir="rtl"] .card { text-align: right; }

[dir="rtl"] .badge { margin-left: 0; margin-right: 4px; }

[dir="rtl"] .messages-wrap .message { text-align: right; }

[dir="rtl"] select {
  background-position: left 10px center;
  padding-left: 32px;
  padding-right: 12px;
}

[dir="rtl"] .pagination { direction: rtl; }

[dir="rtl"] input[type="file"] {
  padding-right: 0;
  padding-left: 0;
}

[dir="rtl"] h1,
[dir="rtl"] h2,
[dir="rtl"] h3,
[dir="rtl"] p,
[dir="rtl"] li { text-align: right; }

[dir="rtl"] .muted { text-align: right; }
