:root {
  --bg: #f4f2ee;
  --ink: #1c1b19;
  --muted: #5c584f;
  --line: #d4cfc4;
  --accent: #1f4d3a;
  --accent-2: #c45c26;
  --card: #fffdf8;
  --danger: #9b1c1c;
  --font: "Segoe UI", system-ui, sans-serif;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: var(--font);
  color: var(--ink);
  background:
    radial-gradient(ellipse at top left, #ebe6dc 0%, transparent 50%),
    linear-gradient(180deg, #f7f4ef 0%, var(--bg) 100%);
  min-height: 100vh;
}

.legal-page {
  max-width: 720px;
  margin: 0 auto;
  padding: 28px 20px 64px;
  line-height: 1.55;
}

.legal-page h1 {
  margin: 8px 0 4px;
  font-size: 1.75rem;
}

.legal-page h2 {
  margin: 28px 0 8px;
  font-size: 1.1rem;
  color: var(--accent);
}

.legal-page ul {
  padding-left: 1.2rem;
}

.legal-page li {
  margin: 0.35rem 0;
}

.topnav {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 12px 20px;
  padding: 12px 20px;
  border-bottom: 1px solid var(--line);
  background: rgba(255, 253, 248, 0.92);
  backdrop-filter: blur(6px);
  position: sticky;
  top: 0;
  z-index: 10;
}

.topnav .brand {
  font-weight: 700;
  letter-spacing: 0.02em;
}

.topnav nav {
  display: flex;
  gap: 14px;
  flex: 1;
}

.topnav a {
  text-decoration: none;
  color: var(--muted);
  font-size: 0.95rem;
}

.topnav a.active,
.topnav a:hover {
  color: var(--accent);
}

.userbox {
  display: flex;
  align-items: center;
  gap: 10px;
  font-size: 0.9rem;
  color: var(--muted);
}

.userbox form { margin: 0; }

button.linkish {
  background: none;
  border: none;
  color: var(--accent);
  cursor: pointer;
  font: inherit;
  text-decoration: underline;
  padding: 0;
}

.page {
  max-width: 1200px;
  margin: 0 auto;
  padding: 24px 20px 48px;
}

h1 { margin: 0 0 8px; font-size: 1.6rem; }
.muted { color: var(--muted); margin-top: 0; }
.error { color: var(--danger); }

.stack {
  display: grid;
  gap: 12px;
}

label {
  display: grid;
  gap: 4px;
  font-size: 0.9rem;
}

input, select, textarea, button {
  font: inherit;
}

input, select, textarea {
  padding: 8px 10px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: #fff;
}

button, .btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 8px 14px;
  border-radius: 6px;
  border: 1px solid var(--accent);
  background: var(--accent);
  color: #fff;
  cursor: pointer;
  text-decoration: none;
}

button.secondary, .btn.secondary {
  background: transparent;
  color: var(--accent);
}

button.danger {
  background: var(--danger);
  border-color: var(--danger);
}

.auth-page {
  display: grid;
  place-items: center;
  padding: 24px;
}

.auth-card {
  width: min(400px, 100%);
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 28px;
  box-shadow: 0 12px 40px rgba(28, 27, 25, 0.08);
}

.auth-card h1 { margin-bottom: 4px; }
.auth-card button { width: 100%; margin-top: 8px; }

.card-list {
  list-style: none;
  padding: 0;
  margin: 20px 0 0;
  display: grid;
  gap: 12px;
  grid-template-columns: repeat(auto-fill, minmax(220px, 1fr));
}

.card-link {
  display: block;
  padding: 20px;
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 10px;
  text-decoration: none;
  color: var(--ink);
  font-weight: 600;
  transition: border-color 0.15s, transform 0.15s;
}

.card-link:hover {
  border-color: var(--accent);
  transform: translateY(-1px);
}

.form-card {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.form-card .card-link {
  margin: 0;
}

.form-card-estimate {
  display: flex;
  flex-wrap: wrap;
  align-items: end;
  gap: 8px;
  padding: 0 4px 4px;
}

.form-card-estimate label {
  display: flex;
  flex-direction: column;
  gap: 4px;
  font-size: 0.85rem;
}

.form-card-estimate input {
  width: 5.5rem;
  padding: 6px 8px;
}

.form-card-meta {
  margin: 0;
  padding: 0 4px 4px;
  font-size: 0.85rem;
}

.toolbar {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  align-items: end;
  margin: 16px 0;
  padding: 12px;
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 10px;
}

.toolbar .field {
  display: grid;
  gap: 4px;
  font-size: 0.85rem;
}

.table-wrap {
  overflow: auto;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: var(--card);
}

table.data {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.9rem;
}

table.data th,
table.data td {
  padding: 8px 10px;
  border-bottom: 1px solid var(--line);
  text-align: left;
  white-space: nowrap;
}

table.data th {
  background: #f0ebe3;
  position: sticky;
  top: 0;
  z-index: 1;
}

table.data tr:hover td {
  background: #faf7f2;
}

table.data td.editable {
  cursor: pointer;
}

table.data input.cell-edit {
  width: 100%;
  min-width: 80px;
  padding: 4px 6px;
}

.col-toggles {
  display: flex;
  flex-wrap: wrap;
  gap: 8px 14px;
  margin: 8px 0 16px;
  font-size: 0.85rem;
}

.col-toggles label {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  grid: none;
}

.hidden-col { display: none !important; }

.panel {
  margin-top: 16px;
  padding: 16px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: var(--card);
}

.panel h2 { margin-top: 0; font-size: 1.1rem; }

.grid-2 {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(180px, 1fr));
  gap: 10px;
}

.status-pill {
  display: inline-block;
  padding: 2px 8px;
  border-radius: 999px;
  font-size: 0.8rem;
  background: #e8e2d8;
}

.status-select {
  appearance: auto;
  border: 1px solid #c8c2b8;
  border-radius: 999px;
  padding: 3px 8px;
  font: inherit;
  font-size: 0.8rem;
  line-height: 1.2;
  cursor: pointer;
  max-width: 11.5rem;
  background: #e8e2d8;
  color: inherit;
}

.status-select:disabled {
  opacity: 0.65;
  cursor: wait;
}

table.data td.status-cell {
  white-space: nowrap;
}

.status-pill.status-completado { background: #d5e8dc; }
.status-pill.status-borrador { background: #f3e2c8; }
.status-pill.status-ingreso-automático,
.status-pill.status-ingreso-manual { background: #dce8f5; }
.status-pill.status-en-reparación { background: #efe0c8; }
.status-pill.status-en-restauración { background: #e8dcc8; }
.status-pill.status-contactado { background: #e6d8f0; }
.status-pill.status-retirado { background: #e0e0e0; }

.status-select.status-completado { background: #d5e8dc; }
.status-select.status-borrador { background: #f3e2c8; }
.status-select.status-ingreso-automático,
.status-select.status-ingreso-manual { background: #dce8f5; }
.status-select.status-en-reparación { background: #efe0c8; }
.status-select.status-en-restauración { background: #e8dcc8; }
.status-select.status-contactado { background: #e6d8f0; }
.status-select.status-retirado { background: #e0e0e0; }

.modal.modal-choice {
  width: min(420px, 100%);
}

.choice-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-top: 12px;
}

.choice-actions button {
  flex: 1 1 auto;
  min-width: 140px;
}

.field-warn {
  display: inline-block;
  padding: 2px 8px;
  border-radius: 999px;
  background: #f6d9a8;
  color: #6b3b00;
  font-weight: 600;
  font-size: 0.85rem;
}

.modal-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(28, 27, 25, 0.45);
  display: grid;
  place-items: center;
  padding: 16px;
  z-index: 50;
}

.modal-backdrop[hidden] { display: none !important; }

.modal {
  width: min(720px, 100%);
  max-height: min(90vh, 900px);
  overflow: auto;
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 12px;
  box-shadow: 0 20px 50px rgba(0, 0, 0, 0.2);
  min-width: 0;
}

.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 14px 16px;
  border-bottom: 1px solid var(--line);
  position: sticky;
  top: 0;
  background: var(--card);
}

.modal-header h2 { margin: 0; font-size: 1.1rem; }

.modal-body {
  padding: 16px;
  min-width: 0;
  overflow-x: hidden;
}

.modal-body h3 { margin: 0 0 8px; font-size: 0.95rem; }

.files-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(140px, 1fr));
  gap: 10px;
  margin-bottom: 10px;
  width: 100%;
  min-width: 0;
}

.file-thumb {
  display: block;
  width: 100%;
  max-width: 100%;
  min-width: 0;
  padding: 0;
  border: none;
  background: transparent;
  cursor: pointer;
  text-align: left;
}

.file-thumb img {
  display: block;
  width: 100%;
  max-width: 100%;
  height: 100px;
  object-fit: cover;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fff;
}

.file-card {
  display: grid;
  gap: 6px;
  padding: 8px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fff;
  min-width: 0;
  max-width: 100%;
  overflow: hidden;
}

.file-card .btn {
  width: 100%;
  max-width: 100%;
}

.file-name {
  font-size: 0.75rem;
  color: var(--muted);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  min-width: 0;
}

.file-label-edit {
  display: grid;
  gap: 2px;
  font-size: 0.8rem;
  min-width: 0;
}

.file-label-edit input {
  width: 100%;
  max-width: 100%;
  min-width: 0;
  box-sizing: border-box;
}

.file-upload {
  display: inline-flex;
  flex-direction: column;
  gap: 4px;
  font-size: 0.85rem;
  margin-top: 6px;
}

.file-upload-camera {
  display: inline-flex;
  flex-direction: row;
  align-items: center;
  gap: 8px;
  margin-left: 8px;
  margin-top: 6px;
  padding: 8px 12px;
  background: #2f5d50;
  color: #fff;
  border: 1px solid #244a40;
  border-radius: 8px;
  cursor: pointer;
  font-weight: 600;
}

.file-upload-camera input[type="file"] {
  max-width: 0.1px;
  max-height: 0.1px;
  opacity: 0;
  overflow: hidden;
  position: absolute;
}

.preview-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(20, 18, 16, 0.72);
  display: grid;
  place-items: center;
  padding: 16px;
  z-index: 80;
}

.preview-backdrop[hidden] { display: none !important; }

.preview-panel {
  width: min(960px, 100%);
  height: min(90vh, 900px);
  background: #111;
  border-radius: 12px;
  border: 1px solid #333;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.preview-toolbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding: 10px 12px;
  background: #1c1b19;
  color: #f5f2ea;
  flex-shrink: 0;
}

.preview-toolbar-actions {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-shrink: 0;
}

.preview-toolbar span {
  font-size: 0.9rem;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.preview-toolbar button,
.preview-action {
  background: transparent;
  border: 1px solid #666;
  color: #fff;
  text-decoration: none;
  padding: 6px 10px;
  border-radius: 6px;
  font-size: 0.85rem;
}

.preview-action[hidden] { display: none !important; }

.preview-content {
  flex: 1;
  min-height: 0;
  display: grid;
  place-items: center;
  background: #0d0d0d;
  overflow: auto;
  padding: 12px;
  -webkit-overflow-scrolling: touch;
}

.preview-content img {
  max-width: 100%;
  max-height: 100%;
  width: auto;
  height: auto;
  object-fit: contain;
}

.preview-content iframe {
  width: 100%;
  height: 100%;
  border: 0;
  background: #fff;
}

.preview-content.preview-pdf-mode {
  display: block;
  place-items: unset;
  background: #3a3a3a;
  padding: 10px;
}

.pdf-pages {
  display: flex;
  flex-direction: column;
  gap: 10px;
  width: 100%;
  max-width: 900px;
  margin: 0 auto;
}

.pdf-page-canvas {
  width: 100%;
  height: auto;
  background: #fff;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.35);
}

.preview-loading,
.preview-error {
  color: #f5f2ea;
  text-align: center;
  padding: 24px 12px;
  margin: 0;
}

.preview-content.preview-text-mode {
  place-items: stretch;
  align-content: start;
  background: #f7f4ef;
  padding: 20px;
}

.preview-text {
  margin: 0;
  width: 100%;
  max-width: 720px;
  margin-inline: auto;
  white-space: pre-wrap;
  word-break: break-word;
  font-family: "Iowan Old Style", "Palatino Linotype", Palatino, Georgia, serif;
  font-size: 1.05rem;
  line-height: 1.55;
  color: #1c1916;
  background: #fff;
  border: 1px solid #d8d0c4;
  border-radius: 8px;
  padding: 20px 22px;
  box-shadow: 0 1px 0 rgba(0, 0, 0, 0.04);
}

.preview-text.empty {
  color: #7a746a;
  font-style: italic;
}

@media (max-width: 640px) {
  .topnav { padding: 10px 12px; }
  .page { padding: 16px 12px 40px; }

  .preview-backdrop {
    padding: 0;
  }

  .preview-panel {
    width: 100%;
    height: 100%;
    max-height: none;
    border-radius: 0;
    border: 0;
  }
}

/* Dashboard */
.page-wide { max-width: 1400px; }

.dash-header {
  display: flex;
  flex-wrap: wrap;
  align-items: flex-end;
  justify-content: space-between;
  gap: 16px;
  margin-bottom: 8px;
}

.dash-view-select {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  align-items: end;
}

.dash-view-select label {
  min-width: 200px;
}

.dash-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(320px, 1fr));
  gap: 16px;
  margin-top: 16px;
}

.dash-widget {
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 14px 16px 16px;
  min-height: 140px;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.dash-widget-head h2 {
  margin: 0;
  font-size: 1.05rem;
}

.dash-widget-body { flex: 1; min-width: 0; }

.dash-table { max-height: 360px; }

.dash-widget:has(.dash-table) {
  grid-column: 1 / -1;
}

.dash-tally {
  display: grid;
  place-items: center;
  padding: 24px 12px;
  text-align: center;
}

.dash-tally-value {
  font-size: 3rem;
  font-weight: 700;
  color: var(--accent);
  line-height: 1.1;
}

.dash-meta {
  color: var(--muted);
  font-size: 0.85rem;
  margin-top: 6px;
}

.dash-avg {
  display: grid;
  gap: 14px;
}

.dash-avg-row {
  padding: 10px 12px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fff;
}

.dash-avg-label {
  font-size: 0.9rem;
  color: var(--muted);
}

.dash-avg-value {
  font-size: 1.85rem;
  font-weight: 700;
  color: var(--accent);
  line-height: 1.2;
  margin-top: 2px;
  font-variant-numeric: tabular-nums;
}

.dash-avg-unit {
  font-size: 0.95rem;
  font-weight: 600;
  color: var(--muted);
}

table.data td.num,
table.data th.num {
  text-align: right;
  font-variant-numeric: tabular-nums;
}

.admin-dash-layout {
  display: grid;
  grid-template-columns: minmax(220px, 280px) 1fr;
  gap: 16px;
  margin-top: 16px;
  align-items: start;
}

.view-list {
  list-style: none;
  padding: 0;
  margin: 0;
  display: grid;
  gap: 6px;
}

.view-item {
  width: 100%;
  text-align: left;
  background: #fff;
  color: var(--ink);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 10px 12px;
  display: grid;
  gap: 2px;
}

.view-item.active,
.view-item:hover {
  border-color: var(--accent);
}

.view-item .muted { font-size: 0.8rem; }

.widgets-editor {
  display: grid;
  gap: 14px;
  margin-top: 14px;
}

.widget-edit-card {
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 14px;
  background: #fff;
  display: grid;
  gap: 10px;
}

.widget-edit-head {
  display: flex;
  flex-wrap: wrap;
  gap: 8px 12px;
  align-items: center;
}

.widget-edit-head .btn-del-widget,
.widget-edit-head .danger {
  margin-left: auto;
}

.check-row,
.check-grid {
  display: flex;
  flex-wrap: wrap;
  gap: 8px 14px;
}

.check-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(140px, 1fr));
}

.check-row label,
.check-grid label,
.inline-check {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  grid: none;
  font-size: 0.9rem;
}

.col-fieldset {
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 10px 12px;
  margin: 0;
}

.col-fieldset legend {
  padding: 0 6px;
  font-size: 0.85rem;
  color: var(--muted);
}

.field-label {
  display: block;
  font-size: 0.9rem;
  margin-bottom: 4px;
}

.btn-row {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

@media (max-width: 860px) {
  .admin-dash-layout {
    grid-template-columns: 1fr;
  }
}
