:root {
  color-scheme: light;
  padding-top: env(safe-area-inset-top, 0px);
  padding-bottom: env(safe-area-inset-bottom, 0px);
  --bg: #f4f6f8;
  --card-bg: #ffffff;
  --border: #dde2e8;
  --text: #1f2933;
  --muted: #6b7684;
  --primary: #2563eb;
  --primary-dark: #1d4ed8;
  --accent: #0d9488;
  --danger: #dc2626;
  --success: #15803d;
  --warn-bg: #fff7ed;
  --warn-border: #fed7aa;
}

html { -webkit-text-size-adjust: 100%; }

* { box-sizing: border-box; }

[hidden] { display: none !important; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  padding-bottom: 3rem;
  overflow-x: hidden;
}

img { max-width: 100%; }

.app-header {
  padding: clamp(1rem, 4vw, 1.5rem) 1rem 1rem;
  text-align: center;
  border-bottom: 1px solid var(--border);
}

.app-header h1 {
  margin: 0 0 0.25rem;
  font-size: clamp(1.2rem, 4.5vw, 1.6rem);
  color: var(--primary-dark);
}

.subtitle {
  margin: 0;
  color: var(--muted);
  font-size: 0.9rem;
}

.container {
  max-width: 720px;
  margin: 0 auto;
  padding: 0 clamp(0.75rem, 4vw, 1rem) 2rem;
  width: 100%;
}

.page { width: 100%; min-width: 0; }

.page-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
  margin-bottom: 0.9rem;
  flex-wrap: wrap;
}

.page-head h2 {
  margin: 0;
  font-size: 1.1rem;
}

.btn-back {
  display: inline-flex;
  align-items: center;
  background: none;
  border: none;
  color: var(--primary);
  font-size: 0.9rem;
  font-weight: 600;
  cursor: pointer;
  padding: 0.4rem 0.3rem;
  min-height: 2.25rem;
}

.btn-back:hover { text-decoration: underline; }

.btn-add {
  display: inline-flex;
  align-items: center;
  gap: 0.3rem;
  background: var(--primary);
  color: #fff;
  border: none;
  border-radius: 999px;
  padding: 0.5rem 1.1rem;
  font-size: 0.85rem;
  font-weight: 600;
  cursor: pointer;
  min-height: 2.5rem;
  white-space: nowrap;
  text-decoration: none;
}

.btn-add:hover { background: var(--primary-dark); }

.export-actions {
  display: flex;
  gap: 0.5rem;
  flex-wrap: wrap;
}

.btn-add-secondary {
  background: #fff;
  color: var(--primary);
  border: 1px solid var(--primary);
}

.btn-add-secondary:hover { background: #eef2ff; }

h2 {
  font-size: 1.1rem;
  margin: 0 0 0.75rem;
}

.section-title {
  font-size: 0.95rem;
  margin: 1.1rem 0 0.6rem;
}

.field-label {
  display: block;
  margin: 0.9rem 0 0.35rem;
  font-size: 0.85rem;
  font-weight: 600;
  color: var(--muted);
}

.search-input {
  display: block;
  width: 100%;
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 0.6rem 0.8rem;
  font-size: 16px;
  margin-bottom: 1rem;
  background: #fff;
  color: var(--text);
}

.search-input:focus {
  outline: 2px solid var(--primary);
  outline-offset: 1px;
}

.list {
  display: flex;
  flex-direction: column;
  gap: 0.6rem;
}

.list-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
  background: var(--card-bg);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 0.85rem 1rem;
  cursor: pointer;
  text-align: left;
  width: 100%;
  min-width: 0;
  font: inherit;
  color: inherit;
  min-height: 3rem;
}

.list-item:hover, .list-item:focus-visible {
  border-color: var(--primary);
  background: #f8faff;
}

.list-item-main { min-width: 0; }

.list-item-title {
  font-weight: 600;
  margin: 0 0 0.2rem;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

#list-substations .list-item-title {
  color: var(--primary-dark);
}

.list-item-sub {
  font-weight: 400;
  color: var(--muted);
}

.list-item-meta {
  font-size: 0.8rem;
  color: var(--muted);
}

.list-item-chevron {
  color: var(--muted);
  flex-shrink: 0;
  font-size: 1.2rem;
}

.feeder-summary {
  background: #f8fafc;
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 0.75rem 1rem;
  font-size: 0.9rem;
  line-height: 1.5;
  margin-bottom: 0.5rem;
}

.feeder-summary .pill {
  display: inline-block;
  background: #e0e7ff;
  color: #3730a3;
  border-radius: 999px;
  padding: 0.15rem 0.6rem;
  font-size: 0.75rem;
  margin: 0.15rem 0.25rem 0 0;
}

.muted-note { color: var(--muted); }

.dtc-rows {
  display: flex;
  flex-direction: column;
  gap: 0.6rem;
}

.dtc-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
  background: var(--card-bg);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 0.75rem 1rem;
  min-width: 0;
}

.dtc-row-main { min-width: 0; }

.dtc-row-code { font-weight: 600; }

.dtc-row-name {
  font-size: 0.85rem;
  color: var(--muted);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.dtc-row-meta {
  font-size: 0.78rem;
  color: var(--muted);
  margin-top: 0.15rem;
}

.btn-remove {
  flex-shrink: 0;
  border: 1px solid var(--warn-border);
  background: #fff5f5;
  color: var(--danger);
  border-radius: 8px;
  padding: 0.5rem 0.75rem;
  font-size: 0.8rem;
  font-weight: 600;
  cursor: pointer;
  min-height: 2.5rem;
}

.btn-remove:hover { background: var(--danger); color: #fff; }

.empty-note {
  color: var(--muted);
  font-size: 0.85rem;
  margin: 0;
}

.error-msg {
  color: var(--danger);
  font-size: 0.85rem;
  margin: 0.4rem 0 0;
}

.btn-primary, .btn-secondary, .btn-danger {
  border: none;
  border-radius: 8px;
  padding: 0.65rem 1.1rem;
  font-size: 0.9rem;
  cursor: pointer;
  font-weight: 600;
  min-height: 2.75rem;
}

.btn-primary {
  background: var(--primary);
  color: #fff;
  margin-top: 1rem;
  width: 100%;
}

@media (min-width: 600px) {
  .btn-primary { width: auto; }
}

.btn-primary:hover { background: var(--primary-dark); }

.btn-secondary {
  background: #e5e7eb;
  color: var(--text);
}

.btn-danger {
  background: var(--danger);
  color: #fff;
}

.toast {
  position: fixed;
  bottom: calc(1.25rem + env(safe-area-inset-bottom, 0px));
  left: 50%;
  transform: translateX(-50%);
  background: var(--text);
  color: #fff;
  padding: 0.7rem 1.2rem;
  border-radius: 8px;
  font-size: 0.9rem;
  max-width: 90vw;
  z-index: 50;
}

.toast.success { background: var(--success); }
.toast.error { background: var(--danger); }

.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(15, 23, 42, 0.45);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 1rem;
  z-index: 100;
}

.modal {
  background: #fff;
  border-radius: 10px;
  padding: 1.25rem 1.4rem;
  max-width: 480px;
  width: 100%;
  max-height: 85vh;
  overflow-y: auto;
}

.modal h3 { margin: 0 0 0.75rem; color: var(--primary-dark); }

.modal-actions {
  display: flex;
  flex-wrap: wrap;
  justify-content: flex-end;
  gap: 0.6rem;
  margin-top: 1.1rem;
}

@media (max-width: 420px) {
  .modal { padding: 1rem; }
  .modal-actions { flex-direction: column-reverse; }
  .modal-actions button { width: 100%; }
}

.confirm-list {
  font-size: 0.85rem;
  padding-left: 1.1rem;
  margin: 0;
}

.option-warn {
  font-size: 0.75rem;
  color: #c2410c;
}

/* Tom Select controls: full width, touch-friendly, no iOS input zoom */
.ts-wrapper {
  width: 100% !important;
  position: relative;
  z-index: 1;
}

/* Raise the open dropdown's stacking context above later siblings (e.g. the
   next field in the same page), otherwise it paints underneath them. */
.ts-wrapper.dropdown-active {
  z-index: 30;
}

.ts-wrapper .ts-control {
  width: 100%;
  min-height: 2.75rem;
  font-size: 16px;
  padding: 0.4rem 0.6rem;
  background-color: #ffffff;
}

.ts-wrapper.multi .ts-control > div {
  font-size: 0.85rem;
}

.ts-dropdown {
  font-size: 0.9rem;
  z-index: 30;
  background-color: #ffffff;
  border-color: var(--border);
  box-shadow: 0 12px 28px rgba(15, 23, 42, 0.18);
}

.ts-dropdown .ts-dropdown-content {
  background-color: #ffffff;
}

.ts-dropdown .option {
  padding: 0.6rem 0.75rem;
  background-color: #ffffff;
}

.ts-dropdown .option.active {
  background-color: #eef2f7;
}

@media (max-width: 480px) {
  .container { padding-left: 0.75rem; padding-right: 0.75rem; }
}

@media (min-width: 900px) {
  .container { max-width: 760px; }
  .app-header { padding-top: 2rem; }
}

@media (min-width: 1200px) {
  .container { max-width: 820px; }
}
