:root {
  --app-bg: #f4f6fb;
  --app-surface: #ffffff;
  --app-border: #e4e8f0;
  --app-text: #1f2937;
  --app-muted: #6b7280;
  --app-primary: #4f46e5;
  --app-primary-hover: #4338ca;
  --app-primary-soft: #eef0ff;
  --app-success: #059669;
  --app-success-soft: #e6f6f0;
  --app-radius: 14px;
  --app-shadow: 0 1px 2px rgba(16, 24, 40, 0.04), 0 4px 16px rgba(16, 24, 40, 0.06);
}

html {
  font-size: 15px;
  position: relative;
  min-height: 100%;
}

@media (min-width: 768px) {
  html {
    font-size: 16px;
  }
}

body {
  padding-bottom: 2rem;
  background: var(--app-bg);
  color: var(--app-text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI Variable", "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
}

/* Navbar */
.app-navbar {
  background: rgba(255, 255, 255, 0.85);
  backdrop-filter: blur(8px);
  border-bottom: 1px solid var(--app-border);
}

.app-navbar .navbar-brand {
  display: inline-flex;
  align-items: center;
  gap: 0.6rem;
  font-weight: 700;
  letter-spacing: -0.01em;
  color: var(--app-text);
}

.brand-logo {
  display: block;
  height: 44px;
  width: auto;
}

.brand-mark {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 32px;
  height: 32px;
  border-radius: 9px;
  background: linear-gradient(135deg, #6366f1, #4f46e5);
  color: #fff;
}

.app-navbar .nav-link {
  color: var(--app-muted);
  font-weight: 500;
  border-radius: 8px;
  padding: 0.4rem 0.75rem;
}

.app-navbar .nav-link:hover {
  color: var(--app-text);
  background: var(--app-bg);
}

/* Page header */
.page-header {
  margin: 1.75rem 0 1.25rem;
}

.page-header h1 {
  font-size: 1.6rem;
  font-weight: 700;
  letter-spacing: -0.02em;
  margin: 0;
}

.page-header p {
  color: var(--app-muted);
  margin: 0.25rem 0 0;
}

/* Cards */
.app-card {
  background: var(--app-surface);
  border: 1px solid var(--app-border);
  border-radius: var(--app-radius);
  box-shadow: var(--app-shadow);
}

.app-card-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  padding: 1rem 1.5rem;
  border-bottom: 1px solid var(--app-border);
}

.app-card-header h2 {
  font-size: 1.05rem;
  font-weight: 650;
  margin: 0;
}

.app-card-body {
  padding: 1.5rem;
}

.count-pill {
  background: var(--app-primary-soft);
  color: var(--app-primary);
  font-weight: 600;
  font-size: 0.8rem;
  padding: 0.2rem 0.65rem;
  border-radius: 999px;
}

/* Forms */
.form-label {
  font-size: 0.85rem;
  font-weight: 600;
  color: #374151;
  margin-bottom: 0.4rem;
}

.form-hint {
  font-size: 0.78rem;
  color: var(--app-muted);
  margin-top: 0.35rem;
}

.form-control {
  border-radius: 10px;
  border-color: #d5dbe6;
  padding: 0.6rem 0.85rem;
}

.form-control::placeholder {
  color: #a3aab8;
}

.form-control:focus {
  border-color: var(--app-primary);
  box-shadow: 0 0 0 4px rgba(79, 70, 229, 0.15);
}

.form-control[type="file"] {
  padding: 0.45rem 0.6rem;
}

.form-control[type="file"]::file-selector-button {
  border: 0;
  border-radius: 8px;
  background: var(--app-primary-soft);
  color: var(--app-primary);
  font-weight: 600;
  padding: 0.4rem 0.85rem;
  margin-right: 0.85rem;
  cursor: pointer;
}

.form-check-input:checked {
  background-color: var(--app-primary);
  border-color: var(--app-primary);
}

.form-check-input:focus {
  border-color: var(--app-primary);
  box-shadow: 0 0 0 4px rgba(79, 70, 229, 0.15);
}

.form-switch {
  padding-left: 3.4em;
}

.form-switch .form-check-input {
  width: 2.5em;
  height: 1.3em;
  margin-left: -3.4em;
  cursor: pointer;
}

.form-switch .form-check-label {
  cursor: pointer;
  font-weight: 500;
}

.mono {
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  letter-spacing: 0.02em;
}

/* Buttons */
.btn {
  border-radius: 10px;
  font-weight: 600;
  padding: 0.55rem 1.1rem;
}

.btn-primary {
  background: var(--app-primary);
  border-color: var(--app-primary);
}

.btn-primary:hover,
.btn-primary:focus {
  background: var(--app-primary-hover);
  border-color: var(--app-primary-hover);
}

.btn-soft {
  background: var(--app-primary-soft);
  color: var(--app-primary);
  border: 0;
}

.btn-soft:hover {
  background: #e0e3ff;
  color: var(--app-primary-hover);
}

.btn-sm {
  padding: 0.3rem 0.75rem;
  font-size: 0.85rem;
}

.btn:focus,
.btn:active:focus {
  box-shadow: 0 0 0 4px rgba(79, 70, 229, 0.2);
}

/* Alerts */
.validation-summary-errors ul,
.alert-form-errors ul {
  margin: 0;
  padding-left: 1.1rem;
}

.alert-form-errors {
  background: #fef2f2;
  border: 1px solid #fecaca;
  color: #b91c1c;
  border-radius: 10px;
  padding: 0.75rem 1rem;
  margin-bottom: 1.25rem;
}

.alert-form-errors:empty,
.validation-summary-valid {
  display: none;
}

/* Table */
.app-table {
  margin: 0;
  --bs-table-bg: transparent;
}

.app-table thead th {
  font-size: 0.72rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--app-muted);
  background: #fafbfd;
  border-bottom: 1px solid var(--app-border);
  padding: 0.85rem 1rem;
  white-space: nowrap;
}

.app-table tbody td {
  padding: 0.95rem 1rem;
  vertical-align: middle;
  border-color: var(--app-border);
}

.app-table tbody tr:hover {
  background: #fafbff;
}

.app-table tbody tr:last-child td {
  border-bottom: 0;
}

.file-link {
  display: inline-flex;
  align-items: center;
  gap: 0.4rem;
  color: var(--app-primary);
  font-weight: 500;
  text-decoration: none;
  word-break: break-all;
}

.file-link:hover {
  color: var(--app-primary-hover);
  text-decoration: underline;
}

.status-badge {
  display: inline-flex;
  align-items: center;
  gap: 0.35rem;
  font-size: 0.78rem;
  font-weight: 600;
  padding: 0.25rem 0.65rem;
  border-radius: 999px;
}

.status-badge::before {
  content: "";
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: currentColor;
}

.status-on {
  background: var(--app-success-soft);
  color: var(--app-success);
}

.status-off {
  background: #f1f3f7;
  color: var(--app-muted);
}

.stat-chip {
  display: inline-block;
  background: var(--app-primary-soft);
  color: var(--app-primary);
  font-weight: 600;
  padding: 0.15rem 0.55rem;
  border-radius: 8px;
}

/* Shown at the image's own shape: portrait stays portrait, landscape stays landscape. */
.entry-thumb {
  display: block;
  width: auto;
  height: auto;
  max-width: 160px;
  max-height: 96px;
  border-radius: 8px;
  border: 1px solid var(--app-border);
  background: #fafbfd;
}

a:hover > .entry-thumb {
  border-color: var(--app-primary);
}

.entry-thumb-empty {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 64px;
  height: 64px;
  color: #c3c9d6;
}

.cell-input {
  min-width: 90px;
  max-width: 160px;
  padding: 0.4rem 0.6rem;
}

.alert-flash {
  border-radius: 10px;
  padding: 0.7rem 1rem;
  margin-bottom: 1rem;
  font-weight: 500;
}

.alert-flash-ok {
  background: var(--app-success-soft);
  border: 1px solid #b7e4d3;
  color: var(--app-success);
}

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

.empty-state svg {
  color: #c3c9d6;
  margin-bottom: 0.75rem;
}

/* Footer */
.footer {
  position: absolute;
  bottom: 0;
  width: 100%;
  line-height: 60px;
  border-top: 1px solid var(--app-border) !important;
  font-size: 0.85rem;
}

.footer a {
  color: var(--app-muted);
}

/* Value position picker (Add / Edit) */
.picker-stage {
  position: relative;
  display: block;
  line-height: 0;
  border: 1px solid var(--app-border);
  border-radius: 8px;
  overflow: hidden;
  background: #fff;
  user-select: none;
  touch-action: none;
}

.picker-stage img {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  object-fit: fill; /* EinkConvert stretches the image to the display's shape */
}

.picker-box {
  position: absolute;
  border: 2px solid var(--box-color);
  background: color-mix(in srgb, var(--box-color) 22%, transparent);
  cursor: move;
  box-sizing: border-box;
}

.picker-box-1 { --box-color: #4f46e5; }
.picker-box-2 { --box-color: #059669; }
.picker-box-3 { --box-color: #d97706; }

.picker-box-label {
  position: absolute;
  top: 0;
  left: 0;
  padding: 0 6px;
  font-size: 12px;
  line-height: 18px;
  color: #fff;
  background: var(--box-color);
  white-space: nowrap;
  pointer-events: none;
}

.picker-box-handle {
  position: absolute;
  right: -1px;
  bottom: -1px;
  width: 14px;
  height: 14px;
  background: var(--box-color);
  cursor: nwse-resize;
}

/* Add Entry: sectioned form */
.back-link {
  display: inline-flex;
  align-items: center;
  gap: 0.25rem;
  font-size: 0.85rem;
  font-weight: 500;
  color: var(--app-muted);
  text-decoration: none;
}

.back-link:hover {
  color: var(--app-primary);
}

.add-page {
  max-width: 820px;
  margin-left: auto;
  margin-right: auto;
}

.add-form {
  max-width: 820px;
}

.form-section {
  margin-bottom: 1.25rem;
}

.section-head {
  display: flex;
  align-items: center;
  gap: 0.85rem;
  padding: 1rem 1.5rem;
  border-bottom: 1px solid var(--app-border);
}

.section-head h2 {
  font-size: 1.05rem;
  font-weight: 650;
  margin: 0;
}

.section-head p {
  font-size: 0.82rem;
  color: var(--app-muted);
  margin: 0.1rem 0 0;
}

.step-badge {
  flex: none;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 30px;
  height: 30px;
  border-radius: 50%;
  background: var(--app-primary-soft);
  color: var(--app-primary);
  font-size: 0.9rem;
  font-weight: 700;
}

.form-actions {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  padding: 1rem 1.5rem;
  position: sticky;
  bottom: 1rem;
  z-index: 5;
}

.form-actions .btn-primary {
  padding-left: 1.5rem;
  padding-right: 1.5rem;
}

.form-section .form-select {
  border-radius: 10px;
  border-color: #d5dbe6;
  padding: 0.6rem 0.85rem;
}

.form-section .form-select:focus {
  border-color: var(--app-primary);
  box-shadow: 0 0 0 4px rgba(79, 70, 229, 0.15);
}

.form-section .text-danger,
.form-section .field-validation-error {
  font-size: 0.78rem;
}

/* The section header already titles the picker */
.form-section #valuePicker {
  margin-bottom: 0 !important;
}

.form-section #valuePicker > .form-label {
  display: none;
}

.form-section #pickerStatus:not(:empty) {
  display: inline-block;
  background: var(--app-primary-soft);
  color: var(--app-primary);
  font-weight: 500;
  padding: 0.3rem 0.7rem;
  border-radius: 8px;
}

.form-section .picker-stage {
  margin: 0 auto;
  border-radius: 10px;
  box-shadow: var(--app-shadow);
}

@media (max-width: 575.98px) {
  .section-head,
  .form-actions {
    padding-left: 1rem;
    padding-right: 1rem;
  }

  .app-card-body {
    padding: 1rem;
  }

  .form-actions {
    position: static;
  }

  .form-actions .d-flex {
    width: 100%;
  }

  .form-actions .btn {
    flex: 1;
  }
}
