:root {
  --os-bg: #ffffff;
  --os-sidebar: #ffe8dc;
  --os-surface: #ffffff;
  --os-primary: #ff4d00;
  --os-primary-hover: #d93e00;
  --os-primary-soft: #fff0e8;
  --os-text: #2a1c16;
  --os-muted: #6d564c;
  --os-brand: #ff4d00;
  --os-gold: #ff8a1f;
  --os-border: #f0d2c4;
  --os-danger: #b42318;
  --os-success: #157347;
  --os-warning: #9a6700;
  --os-info: #175cd3;
  --os-radius: 16px;
  --os-header-h: 64px;
  --os-sidebar-w: 250px;
  --os-focus: 0 0 0 3px rgba(255, 77, 0, 0.28);
  --bs-primary: #ff4d00;
  --bs-primary-rgb: 255, 77, 0;
  --bs-link-color: #ff4d00;
  --bs-link-hover-color: #d93e00;
  --bs-focus-ring-color: rgba(255, 77, 0, 0.28);
}

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

html, body {
  max-width: 100%;
  overflow-x: hidden;
}

body {
  margin: 0;
  font-family: "Segoe UI", system-ui, -apple-system, sans-serif;
  color: var(--os-text);
  background: var(--os-bg);
  line-height: 1.45;
}

img, svg, video { max-width: 100%; height: auto; }

a { color: var(--os-primary); }

.skip-link {
  position: absolute;
  left: -999px;
  top: 0;
  background: #000;
  color: #fff;
  padding: 0.5rem 1rem;
  z-index: 10000;
}
.skip-link:focus { left: 0.5rem; top: 0.5rem; }

:focus-visible {
  outline: 3px solid var(--os-primary);
  outline-offset: 2px;
}

.btn, .form-control, .form-select, .nav-link, .icon-btn {
  min-height: 44px;
}

.form-control, .form-select, textarea.form-control {
  font-size: 16px;
  border-radius: 10px;
  background: var(--os-primary-soft);
  border: 1px solid transparent;
}

.form-control:focus, .form-select:focus {
  background: #fff;
  box-shadow: var(--os-focus);
  border-color: var(--os-primary);
}
.form-check-input:checked {
  background-color: var(--os-primary);
  border-color: var(--os-primary);
}
.form-check-input:focus {
  box-shadow: var(--os-focus);
  border-color: var(--os-primary);
}

.btn {
  border-radius: 10px;
  font-weight: 600;
  padding: 0.55rem 1rem;
}

.btn-primary, .btn-os {
  background: var(--os-primary);
  border-color: var(--os-primary);
  color: #fff;
}
.btn-primary:hover, .btn-os:hover {
  background: var(--os-primary-hover);
  border-color: var(--os-primary-hover);
  color: #fff;
}

.btn-outline-os {
  color: var(--os-primary);
  border: 1px solid var(--os-primary);
  background: #fff;
}
.btn-outline-os:hover {
  background: var(--os-primary-soft);
  color: var(--os-primary-hover);
  border-color: var(--os-primary-hover);
}

.btn:disabled,
.btn[aria-disabled="true"] {
  cursor: not-allowed;
}

/* Auth */
.auth-body {
  min-height: 100dvh;
  display: grid;
  place-items: center;
  padding: 1.25rem;
  padding-bottom: max(1.25rem, env(safe-area-inset-bottom));
  background:
    radial-gradient(1200px 500px at 80% 110%, #ff8a4a 0%, transparent 60%),
    linear-gradient(180deg, #fff7f2 0%, #ffe4d4 55%, #ffb089 100%);
  position: relative;
}
.auth-body::after {
  content: "";
  position: absolute;
  inset: auto 0 0 0;
  height: 38%;
  background: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 1440 320'%3E%3Cpath fill='%23ff4d00' fill-opacity='0.22' d='M0,192L80,176C160,160,320,128,480,133.3C640,139,800,181,960,181.3C1120,181,1280,139,1360,117.3L1440,96L1440,320L0,320Z'%3E%3C/path%3E%3Cpath fill='%23d93e00' fill-opacity='0.28' d='M0,256L120,240C240,224,480,192,720,197.3C960,203,1200,245,1320,266.7L1440,288L1440,320L0,320Z'%3E%3C/path%3E%3C/svg%3E") bottom/cover no-repeat;
  pointer-events: none;
}
.auth-card {
  width: min(100%, 480px);
  background: #fff;
  border-radius: 22px;
  padding: 2rem 1.5rem 1.5rem;
  box-shadow: 0 20px 50px rgba(255, 77, 0, 0.12);
  position: relative;
  z-index: 1;
}
.auth-brand {
  display: grid;
  justify-items: center;
  gap: 0.75rem;
  margin-bottom: 1.5rem;
}
.auth-logo {
  display: block;
  width: 96px;
  height: 96px;
  object-fit: cover;
  border-radius: 18px;
  box-shadow: 0 10px 24px rgba(255, 77, 0, 0.25);
}
.auth-wordmark {
  margin: 0;
  font-size: 1.35rem;
  font-weight: 800;
  letter-spacing: -0.03em;
  color: var(--os-primary);
}
.password-wrap { position: relative; }
.password-wrap .toggle-password {
  position: absolute;
  right: 6px;
  top: 50%;
  transform: translateY(-50%);
  border: 0;
  background: transparent;
  width: 44px;
  height: 44px;
  color: var(--os-muted);
}

/* App shell */
.app-shell {
  min-height: 100dvh;
  display: flex;
  background: var(--os-bg);
}
.sidebar {
  width: var(--os-sidebar-w);
  background: var(--os-sidebar);
  padding: 1rem 0.85rem;
  display: none;
  flex-direction: column;
  gap: 1rem;
  position: sticky;
  top: 0;
  height: 100dvh;
  overflow-y: auto;
}
.sidebar-brand {
  display: flex;
  align-items: center;
  gap: 0.7rem;
  padding: 0.35rem 0.5rem 0.75rem;
  text-decoration: none;
  color: inherit;
}
.sidebar-brand img,
.brand-mark {
  width: 44px;
  height: 44px;
  object-fit: cover;
  border-radius: 10px;
  flex: none;
  box-shadow: 0 4px 12px rgba(255, 77, 0, 0.2);
}
.brand-name {
  font-weight: 800;
  letter-spacing: -0.03em;
  line-height: 1.15;
  color: var(--os-primary);
}
.nav-section-label {
  font-size: 0.78rem;
  color: var(--os-muted);
  padding: 0 0.85rem 0.35rem;
}
.side-link {
  display: flex;
  align-items: center;
  gap: 0.7rem;
  padding: 0.7rem 0.9rem;
  min-height: 44px;
  color: var(--os-text);
  text-decoration: none;
  border-radius: 999px;
  font-weight: 500;
}
.side-link:hover { background: rgba(255,255,255,0.55); color: var(--os-text); }
.side-link.is-active {
  background: #fff;
  color: var(--os-primary);
  box-shadow: 0 1px 2px rgba(255, 77, 0, 0.08);
}
.side-link svg { width: 18px; height: 18px; flex: none; }
.side-sub {
  padding-left: 2.4rem;
  font-size: 0.92rem;
}

.app-main { flex: 1; min-width: 0; display: flex; flex-direction: column; }
.app-header {
  min-height: var(--os-header-h);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
  padding: 0.5rem 1rem;
  position: sticky;
  top: 0;
  z-index: 20;
  background: var(--os-bg);
}
.context-chip {
  display: flex;
  flex-wrap: wrap;
  gap: 0.35rem;
  font-size: 0.85rem;
  color: var(--os-muted);
}
.context-chip strong { color: var(--os-text); font-weight: 600; }
.icon-btn {
  width: 44px;
  height: 44px;
  border: 0;
  background: transparent;
  border-radius: 999px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}
.icon-btn:hover { background: rgba(0,0,0,0.05); }

.app-content {
  padding: 0 1rem 2rem;
  width: min(100%, 1400px);
  margin: 0 auto;
}

.surface {
  background: var(--os-surface);
  border-radius: var(--os-radius);
  box-shadow: 0 8px 24px rgba(255, 77, 0, 0.06);
  padding: 1rem;
}

.toolbar {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
  margin-bottom: 0.85rem;
}
.toolbar .btn { white-space: nowrap; }

.page-title {
  font-size: 1.15rem;
  font-weight: 700;
  margin: 0 0 0.75rem;
  color: var(--os-text);
}

.badge-success { background: #e8f6ee; color: var(--os-success); }
.badge-warning { background: #fff6d8; color: var(--os-warning); }
.badge-danger { background: #fdecec; color: var(--os-danger); }
.badge-info { background: #e8f1ff; color: var(--os-info); }
.badge-muted { background: #eef0f4; color: var(--os-muted); }
.status-badge {
  display: inline-flex;
  align-items: center;
  border-radius: 999px;
  padding: 0.2rem 0.7rem;
  font-size: 0.82rem;
  font-weight: 600;
}

.table-wrap { overflow-x: auto; }
.data-table {
  width: 100%;
  border-collapse: collapse;
  min-width: 720px;
}
.data-table th {
  text-align: left;
  font-size: 0.78rem;
  letter-spacing: 0.02em;
  color: var(--os-muted);
  font-weight: 700;
  padding: 0.7rem 0.6rem;
  border-bottom: 1px solid var(--os-border);
  white-space: nowrap;
}
.data-table td {
  padding: 0.7rem 0.6rem;
  border-bottom: 1px solid #f7ebe6;
  vertical-align: middle;
  overflow-wrap: anywhere;
}
.data-table tr:hover td { background: var(--os-primary-soft); }

.record-card {
  background: #fff;
  border-radius: 14px;
  padding: 0.9rem 1rem;
  box-shadow: 0 4px 16px rgba(255, 77, 0, 0.06);
  display: grid;
  gap: 0.35rem;
}
.record-card h3 {
  font-size: 1rem;
  margin: 0;
  overflow-wrap: anywhere;
}
.meta { color: var(--os-muted); font-size: 0.9rem; overflow-wrap: anywhere; }
.cards-mobile { display: grid; gap: 0.75rem; }
.table-desktop { display: none; }

.empty-state {
  text-align: center;
  padding: 2.5rem 1rem;
  color: var(--os-muted);
}

.filters-panel,
.collapsible-panel { display: none; }
.filters-panel.is-open,
.collapsible-panel.is-open { display: block; }

.overlay-nav {
  position: fixed;
  inset: 0;
  background: rgba(20, 24, 38, 0.45);
  z-index: 40;
  display: none;
}
.overlay-nav.is-open { display: block; }
.sidebar.is-open {
  display: flex;
  position: fixed;
  z-index: 50;
  left: 0;
  top: 0;
  height: 100dvh;
  width: min(86vw, 280px);
  box-shadow: 8px 0 30px rgba(0,0,0,0.12);
}

.alert { overflow-wrap: anywhere; }

.help-text { color: var(--os-muted); font-size: 0.9rem; }

.user-menu { position: relative; }
.user-menu .dropdown-menu { min-width: 14rem; }

.modal-dialog { width: min(100% - 1.5rem, 640px); }

.import-steps { display: grid; gap: 0.75rem; }

.visually-hidden, .sr-only {
  position: absolute !important;
  width: 1px; height: 1px;
  padding: 0; margin: -1px;
  overflow: hidden; clip: rect(0,0,0,0);
  border: 0;
}

@media (min-width: 576px) {
  .auth-card { padding: 2.25rem; }
  .toolbar-inline { display: flex; gap: 0.5rem; }
}

@media (min-width: 768px) {
  .form-grid-2 {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 0.75rem 1rem;
  }
}

@media (min-width: 992px) {
  .sidebar { display: flex; }
  .menu-toggle { display: none !important; }
  .cards-mobile { display: none; }
  .table-desktop { display: table; }
  .app-content { padding: 0 1.5rem 2.5rem; }
}

@media (min-width: 1400px) {
  .app-content { padding-right: 2rem; }
  .form-narrow { max-width: 720px; }
}

[aria-busy="true"] { opacity: 0.7; pointer-events: none; }

.htmx-indicator { display: none; }
.htmx-request .htmx-indicator, .htmx-indicator.htmx-request { display: inline-flex; }

.admin-sidebar { background: #ffd8c6; }
.admin-chip {
  display: inline-flex;
  align-items: center;
  min-height: 32px;
  padding: 0.2rem 0.7rem;
  border-radius: 999px;
  background: var(--os-primary);
  color: #fff;
  font-size: 0.78rem;
  font-weight: 600;
}
.kpi-grid {
  display: grid;
  gap: 0.75rem;
  grid-template-columns: 1fr;
}
.kpi-card {
  display: flex;
  flex-direction: column;
  gap: 0.25rem;
  background: #fff;
  border-radius: 16px;
  padding: 1rem 1.1rem;
  text-decoration: none;
  color: inherit;
  box-shadow: 0 8px 24px rgba(255, 77, 0, 0.06);
}
.kpi-card:hover { color: inherit; box-shadow: 0 10px 28px rgba(255, 77, 0, 0.12); }
.kpi-label { color: var(--os-muted); font-size: 0.85rem; font-weight: 600; }
.kpi-value { font-size: 1.8rem; line-height: 1.1; color: var(--os-primary); }

@media (min-width: 768px) {
  .kpi-grid { grid-template-columns: 1fr 1fr; }
}
@media (min-width: 1200px) {
  .kpi-grid { grid-template-columns: repeat(4, 1fr); }
}

.omr-frame {
  position: relative;
  width: min(100%, 720px);
  aspect-ratio: 1.414 / 1;
  background: #1a120e;
  border-radius: 16px;
  overflow: hidden;
}
.omr-frame.is-live {
  background: #111;
}
.omr-frame video {
  display: block;
  width: 100%;
  height: 100%;
  object-fit: cover;
  background: #111;
}
.omr-guide {
  position: absolute;
  inset: 8%;
  border: 3px dashed rgba(255, 77, 0, 0.85);
  border-radius: 8px;
  pointer-events: none;
  z-index: 1;
}
.omr-overlay {
  position: absolute;
  inset: 0;
  z-index: 2;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 0.85rem;
  padding: 1.25rem;
  text-align: center;
  background: rgba(26, 18, 14, 0.72);
}
.omr-overlay[hidden] {
  display: none;
}
.omr-status {
  margin: 0;
  color: #fff;
  font-size: 0.95rem;
}
#omr-capture:disabled {
  opacity: 0.55;
  cursor: not-allowed;
}
.omr-score {
  font-size: 1.25rem;
  margin-bottom: 0.35rem;
}
.omr-score strong { color: var(--os-primary); }

.olympiad-name-cell {
  display: flex;
  align-items: center;
  gap: 0.7rem;
}
.olympiad-logo {
  width: 40px;
  height: 40px;
  object-fit: cover;
  border-radius: 10px;
  flex: none;
  background: var(--os-primary-soft);
  box-shadow: 0 2px 8px rgba(255, 77, 0, 0.16);
}
.olympiad-logo-lg {
  width: 72px;
  height: 72px;
}
.olympiad-logo-header {
  width: 32px;
  height: 32px;
}
.olympiad-logo-preview {
  display: flex;
  align-items: center;
  gap: 0.75rem;
}
.text-danger { color: var(--os-danger); }

.gabarito-form {
  display: grid;
  gap: 1rem;
  max-width: 1100px;
}
.gabarito-hero {
  display: flex;
  align-items: center;
  gap: 1rem;
  padding: 1.15rem 1.25rem;
  border-radius: var(--os-radius);
  color: #fff;
  background: linear-gradient(135deg, var(--os-primary) 0%, var(--os-gold) 100%);
  box-shadow: 0 10px 24px rgba(255, 77, 0, 0.18);
}
.gabarito-hero-icon {
  width: 52px;
  height: 52px;
  border-radius: 14px;
  display: grid;
  place-items: center;
  background: rgba(255, 255, 255, 0.18);
  flex: none;
}
.gabarito-hero-title {
  margin: 0;
  font-size: 1.35rem;
  font-weight: 700;
  line-height: 1.2;
}
.gabarito-hero-text {
  margin: 0.3rem 0 0;
  color: rgba(255, 255, 255, 0.92);
  font-size: 0.95rem;
}
.gabarito-card {
  background: var(--os-surface);
  border-radius: var(--os-radius);
  box-shadow: 0 8px 24px rgba(255, 77, 0, 0.06);
  padding: 1.15rem 1.2rem 1.25rem;
}
.gabarito-meta-grid {
  display: grid;
  gap: 0.9rem 1.25rem;
}
.gabarito-field .form-label,
.gabarito-required { font-weight: 600; }
.gabarito-required { color: var(--os-danger); }
.gabarito-info {
  display: flex;
  gap: 0.7rem;
  padding: 0.95rem 1rem;
  border-radius: 14px;
  background: var(--os-primary-soft);
  color: var(--os-text);
  border: 1px solid #ffd8c6;
}
.gabarito-info-icon {
  width: 32px;
  height: 32px;
  border-radius: 999px;
  display: grid;
  place-items: center;
  color: var(--os-primary);
  background: #fff;
  flex: none;
}
.gabarito-info strong { display: block; margin-bottom: 0.2rem; }
.gabarito-info p { margin: 0; color: var(--os-muted); font-size: 0.92rem; }
.gabarito-questions-head {
  display: flex;
  align-items: center;
  gap: 0.55rem;
  margin-bottom: 0.85rem;
}
.gabarito-questions-icon {
  width: 28px;
  height: 28px;
  border-radius: 8px;
  display: grid;
  place-items: center;
  color: var(--os-primary);
  background: var(--os-primary-soft);
}
.gabarito-questions-head h2 {
  margin: 0;
  font-size: 1.05rem;
  font-weight: 700;
}
.gabarito-table {
  width: 100%;
  border-collapse: collapse;
  min-width: 720px;
}
.gabarito-table[data-options="ABCD"] [data-option="E"] { display: none; }
.gabarito-table th {
  text-align: center;
  font-size: 0.78rem;
  letter-spacing: 0.02em;
  color: var(--os-muted);
  font-weight: 700;
  padding: 0.7rem 0.45rem;
  background: var(--os-primary-soft);
  border-bottom: 1px solid var(--os-border);
  white-space: nowrap;
}
.gabarito-table th:first-child { text-align: left; padding-left: 0.8rem; }
.gabarito-table td {
  text-align: center;
  padding: 0.55rem 0.4rem;
  border-bottom: 1px solid #f7ebe6;
  vertical-align: middle;
}
.gabarito-table td:first-child { text-align: left; padding-left: 0.8rem; }
.gabarito-table tbody tr:nth-child(even) td { background: #fffaf7; }
.gabarito-table tbody tr:hover td { background: var(--os-primary-soft); }
.gabarito-number {
  display: inline-flex;
  min-width: 3.1rem;
  justify-content: center;
  padding: 0.28rem 0.55rem;
  border-radius: 999px;
  background: #fff;
  border: 1px solid var(--os-border);
  font-weight: 700;
  font-variant-numeric: tabular-nums;
}
.gabarito-radio {
  display: grid;
  place-items: center;
  min-height: 44px;
  margin: 0;
}
.gabarito-radio .form-check-input,
.gabarito-radio input[type="radio"] {
  width: 1.15rem;
  height: 1.15rem;
  margin: 0;
  cursor: pointer;
  accent-color: var(--os-primary);
}
.gabarito-points {
  max-width: 6.2rem;
  margin: 0 auto;
  text-align: center;
  min-height: 40px;
  padding: 0.3rem 0.45rem;
}
.gabarito-delete {
  width: 44px;
  height: 44px;
  border: 0;
  border-radius: 10px;
  background: #fdecec;
  color: var(--os-danger);
  display: inline-grid;
  place-items: center;
}
.gabarito-delete:hover { background: #f8d4d2; }
.gabarito-questions-foot {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
  margin-top: 0.95rem;
}
.gabarito-add {
  border: 0;
  background: transparent;
  color: var(--os-primary);
  font-weight: 700;
  min-height: 44px;
  padding: 0.3rem 0.2rem;
}
.gabarito-add:hover { color: var(--os-primary-hover); }
.gabarito-totals {
  display: inline-flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.4rem 0.55rem;
  margin: 0;
  padding: 0.45rem 0.85rem;
  border-radius: 999px;
  background: #e8f6ee;
  color: var(--os-success);
  font-weight: 600;
}
.gabarito-totals strong { font-weight: 800; }
.gabarito-submit {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 0.85rem;
}
.gabarito-submit-actions { display: flex; flex-wrap: wrap; gap: 0.5rem; }

@media (min-width: 768px) {
  .gabarito-meta-grid {
    grid-template-columns: 1fr 1fr;
    align-items: start;
  }
  .gabarito-info { grid-column: 2; grid-row: 2 / span 2; align-self: stretch; }
}
