@import url('https://fonts.googleapis.com/css2?family=Montserrat:wght@400;500;600;700;800&display=swap');

:root {
  --ink: #111111;
  --paper: #ffffff;
  --mist: #f4f4f2;
  --line: #deded9;
  --muted: #62625d;
  --soft: #eeeeeb;
  --pending: #171717;
  --pending-bg: #f59e0b;
  --soon: #171717;
  --soon-bg: #f2df00;
  --expired: #991b1b;
  --expired-bg: #dc2626;
  --ok: #166534;
  --ok-bg: #22c55e;
  --surface-muted: #fbfbfa;
  --focus-ring: rgba(17,17,17,.18);
  --radius-control: 11px;
  --radius-card: 16px;
}

* { box-sizing: border-box; scrollbar-color: #aaa9a4 transparent; scrollbar-width: thin; }
*::-webkit-scrollbar { width: 10px; height: 10px; }
*::-webkit-scrollbar-track { background: transparent; }
*::-webkit-scrollbar-thumb { background: #aaa9a4; border: 3px solid transparent; border-radius: 999px; background-clip: content-box; }

html { min-width: 320px; background: var(--mist); }

body {
  margin: 0;
  min-height: 100vh;
  color: var(--ink);
  background:
    linear-gradient(135deg, rgba(255,255,255,.86), rgba(244,244,242,.96)),
    var(--mist);
  font-family: 'Montserrat', Arial, sans-serif;
  font-size: 14px;
  line-height: 1.45;
  letter-spacing: .01em;
  text-transform: uppercase;
}

button, input, select, textarea { font: inherit; }
button { cursor: pointer; }
button:focus-visible, [role="button"]:focus-visible { outline: 3px solid var(--focus-ring); outline-offset: 3px; }
button:disabled, [aria-disabled="true"] { cursor: not-allowed; opacity: .55; }
::selection { color: var(--ink); background: var(--soon-bg); }
.hidden { display: none !important; }

.login-view {
  min-height: 100vh;
  display: grid;
  place-items: center;
  padding: 28px;
}

.login-card {
  width: min(100%, 560px);
  margin: auto;
  padding: 34px 40px 30px;
  background: var(--paper);
  border: 1px solid var(--line);
  border-radius: 22px;
  box-shadow: 0 22px 60px rgba(17,17,17,.08);
}

.login-card .eyebrow,
.eyebrow {
  margin: 0 0 12px;
  color: var(--muted);
  font-size: 11px;
  font-weight: 700;
  letter-spacing: .16em;
  text-transform: uppercase;
}

.login-card h1 {
  max-width: none;
  margin: 0;
  font-size: clamp(25px, 3.5vw, 30px);
  line-height: .98;
  letter-spacing: -.06em;
  word-spacing: .08em;
  text-align: center;
  white-space: nowrap;
  text-transform: uppercase;
}

.login-subtitle {
  margin: 16px 0 24px;
  color: var(--muted);
  font-size: 11px;
  line-height: 1.4;
  white-space: nowrap;
}

.login-card > .muted {
  margin: 16px 0 24px;
  font-size: 12px;
  line-height: 1.4;
  text-align: center;
  white-space: normal;
}

label {
  display: grid;
  gap: 6px;
  color: #30302d;
  font-size: 12px;
  font-weight: 700;
  letter-spacing: .1em;
  text-transform: uppercase;
}

label + label { margin-top: 14px; }

input, select, textarea {
  width: 100%;
  min-height: 48px;
  padding: 0 16px;
  color: var(--ink);
  background: var(--surface-muted);
  border: 1px solid var(--line);
  border-radius: var(--radius-control);
  outline: none;
  transition: border-color .2s, box-shadow .2s, background .2s;
}

textarea {
  min-height: 104px;
  padding-top: 14px;
  resize: vertical;
}

input:focus, select:focus, textarea:focus {
  background: var(--paper);
  border-color: var(--ink);
  box-shadow: 0 0 0 3px rgba(17,17,17,.1);
}

input::placeholder, textarea::placeholder { color: #666660; opacity: 1; }

.primary, .login-card button[type="submit"] {
  width: auto;
  min-height: 48px;
  margin-top: 18px;
  color: #fff;
  background: var(--ink);
  border: 1px solid var(--ink);
  border-radius: 13px;
  font-weight: 800;
  letter-spacing: .04em;
  text-transform: uppercase;
  transition: transform .2s, background .2s;
}

.login-card .primary { display: block; width: 100%; margin-left: auto; margin-right: auto; text-align: center; }
.section-heading .primary { width: auto; margin-top: 0; }

.primary:hover, .login-card button[type="submit"]:hover {
  background: #2b2b2b;
  transform: translateY(-1px);
}

.text-button {
  display: block;
  margin: 18px auto 0;
  padding: 0;
  color: var(--ink);
  background: none;
  border: 0;
  border-bottom: 1px solid var(--ink);
  font-weight: 700;
}

.link-button {
  display: block;
  margin: 14px auto 0;
  padding: 0;
  color: var(--ink);
  background: transparent;
  border: 0;
  border-bottom: 1px solid var(--ink);
  font-weight: 700;
  cursor: pointer;
}

.error {
  min-height: 20px;
  margin: 18px 0 0;
  color: var(--expired);
  font-size: 12px;
}

.shell { width: 100%; max-width: 1920px; margin: 0 auto; padding: 28px clamp(18px, 3vw, 48px) 48px; }
.shell:has(#loginView:not(.hidden)) { min-height: 100vh; display: grid; place-items: center; padding: 22px; }
.shell:has(#sessionLoading:not(.hidden)) { min-height: 100vh; display: grid; place-items: center; padding: 22px; }
.session-loading { display: flex; align-items: center; gap: 12px; color: var(--muted); font-size: 12px; font-weight: 700; letter-spacing: .08em; }
.app { width: 100%; }
.app-footer { display: flex; flex-direction: column; align-items: center; gap: 4px; padding: 20px 12px 0; margin-top: 18px; color: var(--muted); font-size: 11px; letter-spacing: .04em; text-align: center; }
.app-footer span:last-child { font-size: 11px; }
.app-view { padding: 30px 3.1% 48px; }
.app-shell { max-width: 1540px; margin: 0 auto; }

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 28px;
  min-height: 120px;
  padding: 26px 40px;
  color: var(--ink);
  background: var(--paper);
  border: 1px solid var(--line);
  border-radius: 18px;
}

.topbar .eyebrow { color: var(--muted); margin-bottom: 8px; }
.topbar h1 { margin: 0; font-size: clamp(24px, 2.3vw, 36px); line-height: 1.08; letter-spacing: -.04em; text-transform: uppercase; }
.topbar-actions { display: flex; align-items: center; gap: 22px; }
.top-actions { display: flex; align-items: center; gap: 28px; }
.admin-menu { position: relative; }
.menu-toggle { width: 42px; height: 42px; padding: 0; color: var(--ink); background: transparent; border: 1px solid #aaa9a4; border-radius: 10px; font-size: 22px; line-height: 1; }
.menu-toggle:hover, .menu-toggle:focus-visible { background: #dfdfdb; }
.menu-panel { position: absolute; z-index: 5; top: calc(100% + 8px); right: 0; min-width: 190px; padding: 7px; background: #fff; border: 1px solid var(--line); border-radius: 10px; box-shadow: 0 12px 28px rgba(17,17,17,.16); }
.menu-item { width: 100%; padding: 11px 12px; color: var(--ink); background: transparent; border: 0; border-radius: 7px; font-size: 11px; font-weight: 800; text-align: left; text-transform: uppercase; }
.menu-item:hover, .menu-item:focus-visible { background: var(--soft); }
.current-user { color: #fff; background: transparent; padding: 0; font-size: 12px; font-weight: 800; }
.logout-btn { color: #fff; background: none; border: 0; font-weight: 800; text-transform: uppercase; }
.topbar .logout-btn { color: var(--ink); background: #fff; border: 1px solid var(--line); }

.content-grid {
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  gap: 26px;
  align-items: stretch;
}

.panel { min-width: 0; }
.side-panel h2 { margin: 0 0 22px; font-size: 22px; letter-spacing: -.04em; }
.side-panel .muted { font-size: 15px; line-height: 1.55; }
.rule-card { display: grid; gap: 8px; padding: 18px; margin-top: 16px; background: #f7f7f5; border: 1px solid var(--line); border-radius: 12px; }
.rule-card strong { font-size: 11px; text-transform: uppercase; }
.rule-card span { color: var(--muted); font-size: 13px; line-height: 1.45; }

.metrics {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 14px;
  margin: 28px 0 24px;
}

.metric-card {
  min-height: 116px;
  padding: 22px 24px;
  text-align: left;
  color: var(--ink);
  background: #fff;
  border: 1px solid var(--line);
  border-radius: var(--radius-card);
  transition: transform .18s ease, border-color .18s ease, box-shadow .18s ease;
}

.metric-card:hover, .metric-card.active {
  border-color: var(--ink);
  box-shadow: 0 10px 22px rgba(17,17,17,.08);
  transform: translateY(-2px);
}

.metric-card:first-child { color: var(--ink); background: var(--soft); border-color: var(--line); }
.metric-label, .metric-card > span { display: block; color: var(--muted); font-size: 12px; font-weight: 700; letter-spacing: .12em; text-transform: uppercase; }
.metric-card[data-metric-filter="soon"] > span { color: var(--soon); }
.metric-card[data-metric-filter="pending"] > span { color: var(--pending); }
.metric-card[data-metric-filter="expired"] > span { color: #fff; }
.metric-card[data-metric-filter="soon"] { color: var(--soon); background: var(--soon-bg); border-color: transparent; }
.metric-card[data-metric-filter="pending"] { color: var(--pending); background: var(--pending-bg); border-color: transparent; }
.metric-card[data-metric-filter="soon"]:hover, .metric-card[data-metric-filter="soon"].active,
.metric-card[data-metric-filter="pending"]:hover, .metric-card[data-metric-filter="pending"].active { border-color: transparent; }
.metric-card[data-metric-filter="expired"] { color: #fff; background: var(--expired-bg); border-color: var(--expired-bg); }
.metrics strong { display: block; margin-top: 12px; font-size: 36px; font-weight: 800; line-height: 1; letter-spacing: -.04em; font-variant-numeric: tabular-nums; }
.metric-card.is-alerting { animation: expired-pulse 1.4s ease-in-out infinite; }
@keyframes expired-pulse {
  0%, 100% { box-shadow: 0 0 0 0 rgba(220, 38, 38, 0); }
  50% { box-shadow: 0 0 0 8px rgba(220, 38, 38, .28); }
}
@media (prefers-reduced-motion: reduce) {
  .metric-card.is-alerting { animation: none; }
}

.content-card {
  padding: 30px;
  background: #fff;
  border: 1px solid var(--line);
  border-radius: 18px;
}

.section-heading {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 20px;
  padding-bottom: 24px;
  border-bottom: 1px solid var(--line);
}

.section-heading h2 { margin: 0; font-size: 24px; letter-spacing: -.035em; text-transform: uppercase; }
.section-heading .eyebrow { margin-bottom: 10px; }
.new-document-btn { width: auto; min-height: 48px; padding: 0 22px; color: #fff; background: var(--ink); border: 1px solid var(--ink); border-radius: 12px; font-weight: 800; text-transform: uppercase; }
.new-document-btn:hover { background: #2b2b2b; }

.toolbar { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); gap: 12px; margin: 24px 0 16px; }
.toolbar input, .toolbar select { min-height: 50px; }
.documents-list.scrollable { overflow-y: auto; padding-right: 8px; }

.document-row {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto auto;
  align-items: center;
  gap: 20px;
  padding: 20px;
  margin-top: 12px;
  background: #fff;
  border: 1px solid var(--line);
  border-radius: var(--radius-card);
  transition: border-color .2s, box-shadow .2s;
}

.document-row:hover { border-color: #aaa9a4; }
.document-name { font-size: 17px; font-weight: 800; letter-spacing: -.03em; text-transform: uppercase; }
.document-company { margin-top: 8px; color: var(--muted); font-size: 12px; text-transform: uppercase; }
.countdown { display: inline-flex; align-items: center; margin-top: 9px; padding: 4px 9px; border-radius: 999px; font-size: 11px; font-weight: 700; text-transform: uppercase; }
.document-row:has(.status.ok) .countdown { color: #fff; background: var(--ok-bg); }
.document-row:has(.status.soon) .countdown { color: var(--soon); background: var(--soon-bg); }
.document-row:has(.status.pending) .countdown { color: var(--pending); background: var(--pending-bg); }
.document-row:has(.status.expired) .countdown { color: #fff; background: var(--expired-bg); }
.document-status-group { display: flex; align-items: center; gap: 16px; }
.document-date { flex: 0 0 125px; text-align: center; }
.document-date strong, .document-date span { display: block; }
.document-date strong { font-size: 15px; font-variant-numeric: tabular-nums; }
.document-date span { margin-top: 6px; color: var(--muted); font-size: 11px; text-transform: uppercase; white-space: nowrap; }
.status { display: inline-flex; align-items: center; justify-content: center; min-height: 34px; padding: 0 13px; border: 1px solid transparent; border-radius: 999px; font-size: 11px; font-weight: 800; text-transform: uppercase; white-space: nowrap; }
.document-status-group .status { flex: 0 0 190px; }
.status.ok { color: #fff; background: var(--ok-bg); }
.status.soon { color: var(--soon); background: var(--soon-bg); }
.status.pending { color: var(--pending); background: var(--pending-bg); }
.status.expired { color: #fff; background: var(--expired-bg); }
.document-actions { display: flex; gap: 8px; }
.ghost, .delete-btn { min-height: 40px; padding: 0 15px; border-radius: 10px; font-size: 11px; font-weight: 800; text-transform: uppercase; }
.ghost { color: var(--ink); background: var(--soft); border: 1px solid var(--soft); }
.ghost:hover { background: #dfdfdb; }
.delete-btn { color: var(--expired); background: #fff; border: 1px solid #e5aaaa; }
.delete-btn:hover { color: #fff; background: var(--expired); }
.empty { padding: 48px 20px; color: var(--muted); text-align: center; }

dialog { width: min(94vw, 650px); max-height: 90vh; overflow-y: auto; overscroll-behavior: contain; padding: 0; color: var(--ink); background: #fff; border: 1px solid var(--line); border-radius: 18px; box-shadow: 0 24px 64px rgba(0,0,0,.22); scrollbar-color: #aaa9a4 transparent; scrollbar-width: thin; }
dialog::backdrop { background: rgba(17,17,17,.6); backdrop-filter: blur(4px); }
.save-progress-dialog { width: min(260px, calc(100vw - 32px)); padding: 0; color: var(--ink); background: var(--paper); border: 1px solid var(--line); border-radius: 18px; box-shadow: 0 18px 48px rgba(17,17,17,.2); }
.save-progress-content { min-height: 150px; display: grid; place-items: center; align-content: center; gap: 14px; padding: 24px; }
.save-progress-content p { margin: 0; font-size: 15px; font-weight: 700; text-transform: none; }
.save-progress-spinner { width: 32px; height: 32px; border: 3px solid var(--line); border-top-color: var(--ink); border-radius: 50%; animation: save-progress-spin .8s linear infinite; }
@keyframes save-progress-spin { to { transform: rotate(360deg); } }
.dialog-form { padding: 34px; }
.field-hint { color: var(--muted); font-size: 11px; font-weight: 400; letter-spacing: 0; text-transform: none; }
.dialog-form input[type="file"] { min-height: 48px; padding: 9px 12px; font-size: 12px; text-transform: none; }
#docAttachmentMessage { min-height: 0; margin: 0; font-size: 11px; }
.panel-heading { display: flex; justify-content: space-between; gap: 20px; align-items: start; padding-bottom: 24px; margin-bottom: 24px; border-bottom: 1px solid var(--line); }
.panel-heading h2 { margin: 0; font-size: 28px; line-height: 1.12; letter-spacing: -.04em; text-transform: uppercase; }
.panel-heading-actions { display: flex; flex: 0 0 auto; align-items: center; gap: 12px; }
.panel-heading-actions .primary { min-height: 48px; margin: 0; white-space: nowrap; }
.manual-open-btn { min-height: 48px; padding: 0 18px; border-radius: 13px; font-weight: 800; white-space: nowrap; }
.icon-btn { color: var(--ink); background: none; border: 0; font-size: 28px; line-height: 1; }
.muted { color: var(--muted); line-height: 1.6; }
.manual-dialog { width: min(94vw, 900px); max-height: 90vh; overflow-y: auto; }
.manual-content { color: var(--ink); text-transform: none; }
.manual-content .panel-heading { position: sticky; top: 0; z-index: 1; padding-top: 8px; background: #fff; }
.manual-intro { margin: 0 0 24px; font-size: 14px; }
.manual-section { padding: 18px 0; border-top: 1px solid var(--line); }
.manual-section h3 { margin: 0 0 10px; font-size: 15px; letter-spacing: .02em; text-transform: uppercase; }
.manual-section p, .manual-section li { font-size: 13px; line-height: 1.7; text-transform: none; }
.manual-section p { margin: 8px 0 0; }
.manual-section ul, .manual-section ol { display: grid; gap: 8px; padding-left: 22px; margin: 10px 0 0; }
.form-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 18px 14px; }
.form-grid label:first-child, .form-grid label:nth-child(2), .form-grid label:last-child { grid-column: 1 / -1; }
.dialog-actions { display: flex; justify-content: flex-end; gap: 10px; margin-top: 26px; }
.dialog-actions button { min-height: 46px; padding: 0 18px; border-radius: 10px; font-weight: 800; text-transform: uppercase; }
.dialog-actions .primary { width: auto; margin: 0; }
.confirm-dialog { padding: 30px 34px 32px; }
.confirm-dialog .eyebrow { margin: 0 0 10px; }
.confirm-dialog h2 { margin: 0 0 14px; font-size: 24px; letter-spacing: -.04em; text-transform: uppercase; }
.confirm-dialog .muted { margin: 0; }
.confirm-dialog .dialog-actions { margin-top: 24px; }
.success-dialog { width: min(90vw, 480px); }
.success-dialog-content { padding: 32px; text-align: center; }
.success-mark { display: grid; width: 48px; height: 48px; margin: 0 auto 16px; place-items: center; color: #fff; background: var(--ok); border-radius: 50%; font-size: 26px; font-weight: 800; animation: success-mark-pop .42s cubic-bezier(.2,.8,.2,1) both; }
@keyframes success-mark-pop { 0% { opacity: 0; transform: scale(.55); } 65% { opacity: 1; transform: scale(1.12); } 100% { opacity: 1; transform: scale(1); } }
.success-dialog .eyebrow { margin: 0 0 8px; }
.success-dialog h2 { margin: 0 0 10px; font-size: 24px; letter-spacing: -.04em; text-transform: uppercase; }
.success-dialog .muted { margin: 0; }
.success-dialog .dialog-actions { justify-content: center; }

.primary, .ghost, .delete-btn, .new-document-btn, .manual-open-btn, .dialog-actions button { border-radius: var(--radius-control); }

@media (max-width: 920px) {
  .topbar { gap: 18px; padding: 24px 28px; }
  .top-actions { gap: 16px; }
  .document-row { grid-template-columns: minmax(0, 1fr) minmax(250px, auto); }
  .document-actions { grid-column: 1 / -1; justify-self: end; }
}

@media (max-width: 900px) {
  .shell { padding: 18px; }
  .metrics { grid-template-columns: repeat(2, 1fr); }
  .content-grid { grid-template-columns: 1fr; }
  .document-row { grid-template-columns: minmax(0, 1fr) auto; }
  .document-status-group { grid-column: 2; grid-row: 1; }
  .status { justify-self: start; }
  .document-actions { justify-self: end; }
}

@media (max-width: 620px) {
  .login-view { padding: 14px; }
  .login-card { width: 100%; padding: 34px 24px 30px; border-radius: 20px; }
  .login-card h1 { white-space: normal; font-size: clamp(32px, 10vw, 42px); }
  .shell { padding: 12px 10px 28px; }
  .topbar { align-items: flex-start; flex-direction: column; padding: 22px; }
  .topbar-actions { width: 100%; justify-content: space-between; }
  .metrics { gap: 10px; margin: 18px 0; }
  .metric-card { min-height: 104px; padding: 17px 15px; border-radius: 16px; }
  .metric-label { font-size: 11px; letter-spacing: .06em; }
  .metric-card > span { font-size: 11px; letter-spacing: .06em; }
  .metrics strong, .metric-value { font-size: 30px; }
  .content-card { padding: 18px 12px; border-radius: 18px; }
  .section-heading { align-items: flex-start; flex-direction: column; }
  .documents-panel > .panel-heading { align-items: stretch; flex-direction: column; }
  .panel-heading-actions { display: grid; grid-template-columns: 1fr 1fr; width: 100%; gap: 8px; }
  .panel-heading-actions > button { width: 100%; padding-right: 8px; padding-left: 8px; font-size: 11px; }
  .manual-content .panel-heading { position: static; padding-top: 0; }
  .section-heading h2 { font-size: 21px; }
  .new-document-btn { width: 100%; }
  .toolbar { grid-template-columns: 1fr; margin-top: 18px; }
  .document-row { grid-template-columns: minmax(0, 1fr); gap: 13px; padding: 16px; }
  .document-status-group { grid-column: 1; grid-row: auto; }
  .document-status-group { gap: 10px; }
  .document-date { flex-basis: 95px; }
  .document-status-group .status { flex-basis: 160px; padding: 0 8px; font-size: 10px; }
  .document-actions { justify-self: stretch; flex-wrap: wrap; }
  .document-actions button { flex: 1 1 calc(50% - 4px); }
  .form-grid { grid-template-columns: 1fr; }
  .form-grid label:first-child, .form-grid label:nth-child(2), .form-grid label:last-child { grid-column: auto; }
  .dialog-form { padding: 24px 18px; }
  .dialog-actions { flex-direction: column-reverse; }
  .dialog-actions button, .dialog-actions .primary { width: 100%; }
}

@media (max-width: 360px) {
  .document-status-group { gap: 6px; }
  .document-date { flex-basis: 82px; }
  .document-status-group .status { flex-basis: 140px; padding: 0 5px; font-size: 9px; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { scroll-behavior: auto !important; transition-duration: .01ms !important; animation-duration: .01ms !important; animation-iteration-count: 1 !important; }
}

.company-list { display: grid; gap: 8px; margin-top: 20px; border-top: 1px solid var(--line); padding-top: 16px; }
.company-item { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 11px 0; border-bottom: 1px solid var(--line); }
.company-item span { color: var(--ink); font-size: 13px; font-weight: 700; }
.company-item-actions { display: flex; gap: 7px; }
.company-item-actions button { min-height: 34px; padding: 0 10px; border-radius: 8px; font-size: 11px; }
.company-edit-btn { color: var(--ink); background: var(--soft); }
.company-delete-btn { color: #b21d1d; background: var(--paper); border: 1px solid #e9a0a0; }
.history-list { display: grid; max-height: 28vh; overflow: auto; gap: 8px; padding: 4px 0; }
.history-list p { margin: 0; padding: 10px 12px; border: 1px solid var(--line); border-radius: 10px; font-size: 13px; line-height: 1.5; overflow-wrap: anywhere; }
.history-list small { color: var(--muted); }
.backup-item { display: flex; justify-content: space-between; align-items: center; gap: 12px; }
.backup-item button { min-height: 34px; padding: 0 12px; border-radius: 8px; }
