:root {
  --red: #e1140a;
  --ink: #1a1a1a;
  --muted: #6b7280;
  --line: #e5e7eb;
  --bg: #f4f5f7;
  --panel: #ffffff;
  --green: #15803d;
  --green-bg: #dcfce7;
  --amber: #b45309;
  --amber-bg: #fef3c7;
  --radius: 10px;
  --shadow: 0 1px 3px rgba(0,0,0,.08), 0 1px 2px rgba(0,0,0,.04);
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; }
body {
  font-family: "Segoe UI", system-ui, -apple-system, Roboto, Helvetica, Arial, sans-serif;
  color: var(--ink);
  background: var(--bg);
  font-size: 15px;
  line-height: 1.5;
}

/* ---------- Layout ---------- */
.app { display: flex; min-height: 100vh; }
.sidebar {
  width: 280px;
  background: var(--panel);
  border-right: 1px solid var(--line);
  padding: 20px 16px;
  display: flex;
  flex-direction: column;
  gap: 16px;
  position: sticky;
  top: 0;
  height: 100vh;
  overflow-y: auto;
}
.sidebar .brand { display: flex; align-items: center; gap: 10px; }
.sidebar .brand img { width: 130px; height: auto; }
.sidebar .app-title { font-size: 18px; font-weight: 800; color: var(--ink); padding: 2px 0 6px; }
.main { flex: 1; padding: 28px 36px; max-width: 1100px; }

/* ---------- Sidebar list ---------- */
.agreement-list { display: flex; flex-direction: column; gap: 6px; }
.agreement-item {
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 10px 12px;
  cursor: pointer;
  background: #fff;
  transition: border-color .15s, box-shadow .15s;
}
.agreement-item:hover { border-color: #cbd5e1; }
.agreement-item.active { border-color: var(--red); box-shadow: 0 0 0 1px var(--red); }
.agreement-item .ai-name { font-weight: 600; font-size: 14px; }
.agreement-item .ai-meta { font-size: 12px; color: var(--muted); margin-top: 2px; }

/* ---------- Buttons ---------- */
.btn {
  display: inline-flex; align-items: center; gap: 8px;
  border: 1px solid var(--line); background: #fff; color: var(--ink);
  padding: 9px 16px; border-radius: 8px; font-size: 14px; font-weight: 600;
  cursor: pointer; transition: background .15s, border-color .15s, opacity .15s;
  text-decoration: none;
}
.btn:hover { background: #f9fafb; }
.btn.primary { background: var(--red); border-color: var(--red); color: #fff; }
.btn.primary:hover { background: #c10f06; }
.btn.ghost { background: transparent; }
.btn:disabled { opacity: .5; cursor: not-allowed; }
.btn.block { width: 100%; justify-content: center; }
.btn.small { padding: 6px 12px; font-size: 13px; }

/* ---------- Header ---------- */
.page-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 8px; gap: 16px; flex-wrap: wrap; }
.page-head h1 { font-size: 22px; margin: 0; }
.page-head .sub { color: var(--muted); font-size: 14px; margin: 2px 0 0; }
.toolbar { display: flex; gap: 10px; flex-wrap: wrap; }

/* ---------- Cards / sections ---------- */
.card {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 22px 24px;
  margin-bottom: 18px;
}
.card h2 {
  font-size: 15px; text-transform: uppercase; letter-spacing: .04em;
  color: var(--muted); margin: 0 0 16px; font-weight: 700;
}
.card h2 .tag { float: right; }

/* ---------- Forms ---------- */
.grid { display: grid; grid-template-columns: repeat(2, 1fr); gap: 14px 20px; }
.grid.three { grid-template-columns: repeat(3, 1fr); }
.field { display: flex; flex-direction: column; gap: 5px; min-width: 0; }
.field.full { grid-column: 1 / -1; }
.field label { font-size: 13px; font-weight: 600; color: #374151; }
.field input, .field select, .field textarea {
  border: 1px solid var(--line); border-radius: 8px; padding: 9px 11px;
  font: inherit; color: var(--ink); background: #fff;
  width: 100%; max-width: 100%; min-width: 0; /* shrink to fit; clip long <select> text on small screens */
}
/* Grid items must be allowed to shrink below their content (long option text) */
.grid > * { min-width: 0; }
.field input:focus, .field select:focus, .field textarea:focus {
  outline: none; border-color: var(--red); box-shadow: 0 0 0 3px rgba(225,20,10,.12);
}
.field input:disabled, .field select:disabled, .field textarea:disabled {
  background: #f3f4f6; color: #4b5563; cursor: not-allowed;
}
.field .hint { font-size: 12px; color: var(--muted); }
.field .hint.err { color: var(--red); font-weight: 600; }
.field input.invalid { border-color: var(--red); box-shadow: 0 0 0 3px rgba(225,20,10,.12); }
.field .flatpickr-wrapper { width: 100%; }
.field .flatpickr-wrapper > input { width: 100%; }

/* ---------- Calendar (flatpickr) — Material / Google-Forms style ---------- */
.flatpickr-calendar {
  border-radius: 16px;
  box-shadow: 0 6px 24px rgba(0,0,0,.20);
  border: none;
  font-family: inherit;
  margin-top: 6px;
  padding: 6px 0 0; /* no horizontal padding: flatpickr is border-box with a fixed width */
}
.flatpickr-calendar::before, .flatpickr-calendar::after { display: none; } /* hide the little arrow */
.flatpickr-months { padding: 8px 4px 6px; }
.flatpickr-current-month { font-size: 15px; padding-top: 4px; }
.flatpickr-current-month .flatpickr-monthDropdown-months,
.flatpickr-current-month input.cur-year { font-weight: 600; color: var(--ink); }

/* In static mode the calendar sits inside .field, so our generic ".field input"
   focus/border styles would leak onto flatpickr's month/year controls. Neutralize. */
.flatpickr-calendar input,
.flatpickr-calendar input:focus,
.flatpickr-calendar select,
.flatpickr-calendar select:focus {
  box-shadow: none !important;
  border: 0 !important;
  outline: 0 !important;
}
/* Month + year dropdowns clearly clickable */
.flatpickr-current-month .flatpickr-monthDropdown-months,
.flatpickr-current-month .flatpickr-yearDropdown {
  cursor: pointer; border-radius: 8px; padding: 3px 6px;
  font-weight: 600; font-size: 15px; color: var(--ink);
  background: transparent; appearance: auto; -webkit-appearance: auto;
}
.flatpickr-current-month .flatpickr-monthDropdown-months:hover,
.flatpickr-current-month .flatpickr-yearDropdown:hover { background: #f1f3f4; border-radius: 8px; }

/* Round icon-button nav arrows like Material */
.flatpickr-months .flatpickr-prev-month,
.flatpickr-months .flatpickr-next-month { border-radius: 50%; padding: 6px 8px; }
.flatpickr-months .flatpickr-prev-month:hover,
.flatpickr-months .flatpickr-next-month:hover { background: #f1f3f4; }
.flatpickr-months .flatpickr-prev-month:hover svg,
.flatpickr-months .flatpickr-next-month:hover svg { fill: var(--ink); }

span.flatpickr-weekday { color: var(--muted); font-weight: 600; font-size: 12px; }

/* Circular days (Material). Don't touch width — keep flatpickr's grid intact. */
.flatpickr-day {
  border-radius: 50%;
  border: 2px solid transparent;
  color: var(--ink);
}
.flatpickr-day:hover,
.flatpickr-day:focus { background: #f1f3f4; border-color: transparent; }
.flatpickr-day.today { color: var(--red); font-weight: 700; border-color: var(--red); }
.flatpickr-day.today:hover { background: #f1f3f4; color: var(--red); }
.flatpickr-day.selected,
.flatpickr-day.selected:hover,
.flatpickr-day.selected:focus {
  background: var(--red); border-color: var(--red); color: #fff; font-weight: 700;
}
.flatpickr-day.flatpickr-disabled,
.flatpickr-day.prevMonthDay,
.flatpickr-day.nextMonthDay { color: #c7ccd1; }

/* Today button (kept) */
.fp-actions { padding: 6px 10px 10px; }
.fp-today-btn {
  border: none; background: transparent; color: var(--red);
  font: inherit; font-weight: 700; font-size: 14px; padding: 8px 18px;
  border-radius: 999px; cursor: pointer; width: 100%; letter-spacing: .02em;
}
.fp-today-btn:hover { background: #fdeceb; }

/* ---------- Tags ---------- */
.tag {
  display: inline-flex; align-items: center; gap: 5px;
  font-size: 11px; font-weight: 700; text-transform: uppercase; letter-spacing: .03em;
  padding: 3px 9px; border-radius: 999px;
}
.tag.locked { background: #eef2ff; color: #4338ca; }
.tag.op { background: var(--amber-bg); color: var(--amber); }
.tag.signed { background: var(--green-bg); color: var(--green); }
.tag.draft { background: #f1f5f9; color: #475569; }
.tag.sent { background: #e0f2fe; color: #0369a1; }
.tag.co-apex { background: #fde2e0; color: var(--red); }
.tag.co-olat { background: #e0ecff; color: #1d4ed8; }

/* ---------- Status banner ---------- */
.banner {
  display: flex; align-items: center; gap: 12px;
  border-radius: var(--radius); padding: 14px 18px; margin-bottom: 18px;
  font-size: 14px;
}
.banner .icon { font-size: 22px; }
.banner.ok { background: var(--green-bg); color: var(--green); border: 1px solid #bbf7d0; }
.banner.warn { background: var(--amber-bg); color: var(--amber); border: 1px solid #fde68a; }
.banner.info { background: #e0f2fe; color: #0369a1; border: 1px solid #bae6fd; }
.banner strong { font-weight: 700; }

/* ---------- Signature ---------- */
.sig-wrap { display: flex; flex-direction: column; gap: 10px; }
.sig-canvas {
  border: 2px dashed #cbd5e1; border-radius: 10px; background: #fff;
  width: 100%; max-width: 480px; height: 180px; touch-action: none; cursor: crosshair;
}
.sig-preview { max-width: 320px; border: 1px solid var(--line); border-radius: 8px; padding: 8px; background:#fff; }
.sig-row { display: flex; gap: 10px; align-items: center; }

/* ---------- Read-only legal text ---------- */
.legal h3 { font-size: 15px; margin: 18px 0 6px; }
.legal p { margin: 0 0 8px; font-size: 14px; }
.kv { display: grid; grid-template-columns: 200px 1fr; gap: 6px 16px; font-size: 14px; }
.kv .k { color: var(--muted); font-weight: 600; }

/* ---------- Change log ---------- */
.log { list-style: none; padding: 0; margin: 0; }
.log li { font-size: 13px; padding: 8px 0; border-bottom: 1px solid var(--line); color: #374151; }
.log li:last-child { border-bottom: 0; }
.log .when { color: var(--muted); font-size: 12px; }

/* ---------- Misc ---------- */
.empty { text-align: center; color: var(--muted); padding: 60px 20px; }
.empty h2 { color: var(--ink); }
.link-box {
  display: flex; gap: 8px; align-items: center; background: #f8fafc;
  border: 1px solid var(--line); border-radius: 8px; padding: 8px 10px; font-size: 13px;
}
.link-box input { flex: 1; border: 0; background: transparent; font: inherit; color: var(--ink); }
.note { font-size: 13px; color: var(--muted); margin-top: 6px; }
.divider { height: 1px; background: var(--line); margin: 18px 0; border: 0; }
.hidden { display: none !important; }
.sign-shell { max-width: 760px; margin: 0 auto; }

/* ---------- Auth screens ---------- */
.auth-shell {
  margin: auto; text-align: center; padding: 60px 20px; width: 100%;
  display: flex; flex-direction: column; align-items: center;
}
.acct { font-size: 12px; color: var(--muted); border-top: 1px solid var(--line); padding-top: 12px; }
.acct strong { color: var(--ink); word-break: break-all; }

/* ==========================================================================
   Responsive / mobile (phones & small tablets). Layout-only; no logic changes.
   ========================================================================== */
@media (max-width: 820px) {
  /* Sidebar becomes a stacked top section instead of a fixed full-height column */
  .app { flex-direction: column; }
  .sidebar {
    width: auto;
    height: auto;
    position: static;
    border-right: none;
    border-bottom: 1px solid var(--line);
    gap: 12px;
  }
  .sidebar .agreement-list { max-height: 30vh; overflow-y: auto; }

  .main { padding: 18px 16px; max-width: 100%; }

  /* All multi-column field grids collapse to a single column */
  .grid,
  .grid.three { grid-template-columns: 1fr; }

  .page-head { gap: 10px; }
  .page-head h1 { font-size: 19px; }
  /* Toolbar buttons wrap and fill the row for easy tapping */
  .toolbar { width: 100%; }
  .toolbar .btn { flex: 1 1 auto; justify-content: center; }

  .card { padding: 18px 16px; }
  .sign-shell { padding-left: 12px; padding-right: 12px; }
}

@media (max-width: 520px) {
  /* Stack key/value rows (labels above values) so nothing gets squeezed */
  .kv { grid-template-columns: 1fr; gap: 2px 0; }
  .kv .k { margin-top: 8px; }
  .kv .k:first-child { margin-top: 0; }

  .page-head h1 { font-size: 18px; }
  .sig-canvas { height: 160px; }
  /* Keep the calendar within the screen on very narrow devices */
  .flatpickr-calendar { max-width: 96vw; }
}
