/* ==========================================================================
   TFeeder fleet manager
   --------------------------------------------------------------------------
   Previously every rule lived in a <style> block inside base.html, which
   meant the browser re-downloaded it with each page and there was nowhere to
   put anything a single template didn't need. It is a real stylesheet now,
   served and cached like one.

   Conventions:
     .card / .grid          layout primitives
     .badge-*               status pills, coloured by severity
     .btn-*                 actions
     .msg-*                 Django messages
   Severity colour is consistent throughout: green = fine, amber = attention,
   red = acting now. Nothing else uses those three colours, so a red pixel
   anywhere on the page always means the same thing.
   ========================================================================== */

:root {
	--brand:        #667eea;
	--brand-dark:   #5568d3;
	--brand-deep:   #764ba2;

	--ok-bg:        #d4edda;  --ok-fg:      #155724;
	--warn-bg:      #fff3cd;  --warn-fg:    #856404;
	--danger-bg:    #f8d7da;  --danger-fg:  #721c24;
	--info-bg:      #cce5ff;  --info-fg:    #004085;
	--neutral-bg:   #e2e3e5;  --neutral-fg: #383d41;

	--ink:          #333;
	--ink-soft:     #666;
	--ink-faint:    #999;
	--surface:      #fff;
	--surface-alt:  #f8f9fa;
	--line:         #eee;

	--radius:       12px;
	--radius-sm:    8px;
	--shadow:       0 4px 6px rgba(0, 0, 0, .1);
	--shadow-lift:  0 8px 14px rgba(0, 0, 0, .16);
	--mono: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
}

* { margin: 0; padding: 0; box-sizing: border-box; }

body {
	font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
	background: linear-gradient(135deg, var(--brand) 0%, var(--brand-deep) 100%);
	background-attachment: fixed;
	min-height: 100vh;
	padding: 20px;
	color: var(--ink);
	line-height: 1.45;
}

a { color: var(--brand); }
a:hover { color: var(--brand-dark); }

.container { max-width: 1400px; margin: 0 auto; }
.container-narrow { max-width: 900px; margin: 0 auto; }

/* ---------- header ---------- */
.header {
	background: var(--surface);
	padding: 22px 25px;
	border-radius: var(--radius);
	box-shadow: var(--shadow);
	margin-bottom: 22px;
	display: flex;
	justify-content: space-between;
	align-items: center;
	flex-wrap: wrap;
	gap: 15px;
}
.header h1 { font-size: 1.8em; line-height: 1.2; }
.header .sub { color: var(--ink-soft); font-size: .9em; margin-top: 4px; }

.crumbs { font-size: .85em; color: var(--ink-faint); margin-bottom: 4px; }
.crumbs a { text-decoration: none; }

.actions { display: flex; gap: 10px; flex-wrap: wrap; align-items: center; }
.actions form { display: inline; }

/* ---------- buttons ---------- */
.btn {
	padding: 10px 18px;
	border: none;
	border-radius: var(--radius-sm);
	cursor: pointer;
	text-decoration: none;
	font-size: .92em;
	font-weight: 600;
	font-family: inherit;
	display: inline-block;
	transition: background .15s, transform .05s;
}
.btn:active { transform: translateY(1px); }
.btn-primary { background: var(--brand); color: #fff; }
.btn-primary:hover { background: var(--brand-dark); color: #fff; }
.btn-primary:disabled { background: #aab; cursor: not-allowed; }
.btn-ghost { background: var(--line); color: var(--ink); }
.btn-ghost:hover { background: #ddd; color: var(--ink); }
.btn-danger { background: var(--danger-bg); color: var(--danger-fg); }
.btn-danger:hover { background: #f5c6cb; }
.btn-sm { padding: 6px 12px; font-size: .82em; }
.btn-block { width: 100%; text-align: center; }

/* ---------- cards ---------- */
.card {
	background: var(--surface);
	padding: 22px;
	border-radius: var(--radius);
	box-shadow: var(--shadow);
	margin-bottom: 20px;
}
.card h2 {
	font-size: 1.15em;
	margin-bottom: 15px;
	border-bottom: 3px solid var(--brand);
	padding-bottom: 9px;
}
.card h2 .count { color: var(--ink-faint); font-weight: 400; font-size: .85em; }

.grid {
	display: grid;
	grid-template-columns: repeat(auto-fit, minmax(320px, 1fr));
	gap: 18px;
}
.grid-wide { grid-column: 1 / -1; }

/* ---------- badges ---------- */
.badge {
	display: inline-block;
	padding: 4px 11px;
	border-radius: 20px;
	font-size: .78em;
	font-weight: 700;
	white-space: nowrap;
}
.badge-ok      { background: var(--ok-bg);      color: var(--ok-fg); }
.badge-warn    { background: var(--warn-bg);    color: var(--warn-fg); }
.badge-danger  { background: var(--danger-bg);  color: var(--danger-fg); }
.badge-info    { background: var(--info-bg);    color: var(--info-fg); }
.badge-neutral { background: var(--neutral-bg); color: var(--neutral-fg); }

/* ---------- metrics ---------- */
.metric {
	display: flex;
	justify-content: space-between;
	align-items: center;
	padding: 9px 0;
	border-bottom: 1px solid var(--line);
	gap: 10px;
}
.metric:last-child { border-bottom: none; }
.metric-label { color: var(--ink-soft); font-size: .92em; }
.metric-value { font-weight: 700; color: var(--brand); text-align: right; }
.metric-value.muted { color: var(--ink-faint); font-weight: 400; }

.big-number { font-size: 2.4em; font-weight: 700; color: var(--brand); margin: 10px 0; }
.big-number .unit { font-size: .35em; color: var(--ink-faint); font-weight: 400; }

/* ---------- meters ---------- */
.meter {
	width: 100%;
	height: 24px;
	background: var(--line);
	border-radius: 12px;
	overflow: hidden;
	margin: 10px 0;
}
.meter-fill {
	height: 100%;
	border-radius: 12px;
	background: #28a745;
	transition: width .4s;
}
.meter-fill.warn   { background: #ffc107; }
.meter-fill.danger { background: #dc3545; }

/* ---------- forms ---------- */
label { display: block; font-weight: 600; margin-bottom: 6px; font-size: .9em; }
label.inline { display: inline; font-weight: 500; }

input, select, textarea {
	width: 100%;
	padding: 10px;
	border: 2px solid var(--line);
	border-radius: var(--radius-sm);
	font-size: .93em;
	font-family: inherit;
	background: #fff;
	color: var(--ink);
}
input:focus, select:focus, textarea:focus {
	outline: none;
	border-color: var(--brand);
	box-shadow: 0 0 0 3px rgba(102, 126, 234, .12);
}
input[type="checkbox"], input[type="radio"] { width: auto; }
input.invalid { border-color: #dc3545; }
textarea { resize: vertical; }

.form-group { margin-bottom: 16px; }
.form-row { display: grid; grid-template-columns: 1fr 1fr; gap: 14px; }
.form-row-3 { display: grid; grid-template-columns: repeat(3, 1fr); gap: 14px; }
.checkbox-row { display: flex; align-items: center; gap: 10px; margin-bottom: 12px; }
.checkbox-row label { margin: 0; font-weight: 500; }
.help, .help-text { color: var(--ink-faint); font-size: .83em; margin-top: 5px; }

/* ---------- messages ---------- */
.msg {
	padding: 13px 16px;
	border-radius: var(--radius-sm);
	margin-bottom: 16px;
	font-size: .92em;
}
.msg-success, .msg-ok    { background: var(--ok-bg);     color: var(--ok-fg); }
.msg-error,   .msg-danger{ background: var(--danger-bg); color: var(--danger-fg); }
.msg-warning, .msg-warn  { background: var(--warn-bg);   color: var(--warn-fg); }
.msg-info, .msg-debug    { background: var(--info-bg);   color: var(--info-fg); }
.msg a { color: inherit; font-weight: 600; }

/* ---------- events ---------- */
.event-list { max-height: 420px; overflow-y: auto; }
.event {
	padding: 10px 12px;
	margin: 7px 0;
	border-radius: 6px;
	background: var(--surface-alt);
	border-left: 4px solid var(--brand);
	font-size: .9em;
}
.event.fail   { background: #fdf3f4; border-left-color: #dc3545; }
.event.warn   { background: #fffdf3; border-left-color: #ffc107; }
.event.forced { background: #fffdf3; border-left-color: #ffc107; }
.event-reason { color: var(--ink-soft); font-size: .93em; margin-top: 3px; }
.event-time   { color: var(--ink-faint); font-size: .8em; }

/* ---------- tables ---------- */
.table { width: 100%; border-collapse: collapse; font-size: .9em; }
.table th, .table td {
	padding: 10px 12px;
	text-align: left;
	border-bottom: 1px solid var(--line);
}
.table th {
	color: var(--ink-soft);
	font-size: .82em;
	text-transform: uppercase;
	letter-spacing: .03em;
	white-space: nowrap;
}
.table tbody tr:hover { background: var(--surface-alt); }
.table td.num { text-align: right; font-variant-numeric: tabular-nums; }
.table-wrap { overflow-x: auto; }

/* ---------- device cards ---------- */
.device-card {
	background: var(--surface);
	border-radius: var(--radius);
	padding: 20px;
	box-shadow: var(--shadow);
	display: flex;
	flex-direction: column;
	gap: 12px;
	transition: transform .15s, box-shadow .15s;
}
.device-card:hover { transform: translateY(-3px); box-shadow: var(--shadow-lift); }
.device-card.alerting { border-left: 5px solid #dc3545; }
.device-card.offline  { opacity: .72; }

.device-head { display: flex; justify-content: space-between; align-items: flex-start; gap: 10px; }
.device-head h3 { font-size: 1.1em; }
.device-head h3 a { text-decoration: none; }
.device-head .id { color: var(--ink-faint); font-size: .78em; font-family: var(--mono); }

.readings { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; }
.reading { background: var(--surface-alt); border-radius: var(--radius-sm); padding: 8px; text-align: center; }
.reading .v { font-weight: 700; color: var(--brand); font-size: 1.05em; }
.reading .k { color: var(--ink-faint); font-size: .7em; text-transform: uppercase; }
.reading.bad .v  { color: #dc3545; }
.reading.warn .v { color: #b8860b; }

.alerts { display: flex; flex-wrap: wrap; gap: 6px; }

/* ---------- stat strip ---------- */
.stats { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: 14px; margin-bottom: 20px; }
.stat { background: var(--surface); border-radius: var(--radius); padding: 16px 18px; box-shadow: var(--shadow); }
.stat .v { font-size: 1.9em; font-weight: 700; color: var(--brand); }
.stat .k { color: var(--ink-soft); font-size: .84em; }
.stat.alert .v { color: #dc3545; }

/* ---------- snapshots ---------- */
.snapshot-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(170px, 1fr)); gap: 12px; }
.snapshot { border: 1px solid var(--line); border-radius: var(--radius-sm); overflow: hidden; background: var(--surface-alt); position: relative; }
.snapshot img { width: 100%; height: 130px; object-fit: cover; display: block; cursor: pointer; }
.snapshot .noimg {
	height: 130px; display: flex; align-items: center; justify-content: center;
	color: var(--ink-faint); font-size: .78em; text-align: center; padding: 10px;
}
.snapshot .meta { padding: 7px 9px; font-size: .76em; color: var(--ink-soft); }
.snapshot .conf {
	position: absolute; top: 6px; right: 6px;
	background: rgba(220, 53, 69, .92); color: #fff;
	padding: 2px 7px; border-radius: 10px; font-size: .7em; font-weight: 700;
}

/* ---------- lightbox ---------- */
.lightbox {
	display: none; position: fixed; inset: 0;
	background: rgba(0, 0, 0, .87); z-index: 100;
	align-items: center; justify-content: center; padding: 20px;
}
.lightbox.open { display: flex; }
.lightbox img { max-width: 92vw; max-height: 82vh; border-radius: var(--radius-sm); }
.lightbox .caption { color: #fff; text-align: center; margin-top: 12px; font-size: .9em; }

/* ---------- charts ---------- */
.chart-card { background: var(--surface); border-radius: var(--radius); padding: 20px; box-shadow: var(--shadow); margin-bottom: 18px; }
.chart-card h3 { font-size: 1em; margin-bottom: 4px; }
.chart-card .range { color: var(--ink-faint); font-size: .8em; margin-bottom: 12px; }
.chart { width: 100%; height: 170px; display: block; }
.chart-empty { color: var(--ink-faint); text-align: center; padding: 40px 0; font-size: .9em; }

/* ---------- pagination ---------- */
.pager { display: flex; gap: 8px; align-items: center; justify-content: center; margin-top: 18px; flex-wrap: wrap; }
.pager .current { color: var(--ink-soft); font-size: .88em; padding: 0 8px; }

/* ---------- misc ---------- */
code, .mono {
	font-family: var(--mono);
	background: #f4f5fa;
	padding: 2px 6px;
	border-radius: 4px;
	font-size: .88em;
	word-break: break-all;
}
pre.mono { display: block; padding: 16px; white-space: pre-wrap; line-height: 1.7; }
.empty { color: var(--ink-faint); text-align: center; padding: 30px; }
.footer { text-align: center; color: #fff; padding: 18px; font-size: .85em; opacity: .9; }
.footer a { color: #fff; }
.nowrap { white-space: nowrap; }
.text-muted { color: var(--ink-faint); }
.mt-0 { margin-top: 0; } .mt-1 { margin-top: 10px; } .mt-2 { margin-top: 20px; }
.mb-0 { margin-bottom: 0; } .mb-1 { margin-bottom: 10px; }

/* ---------- auth ---------- */
.auth-wrap { max-width: 420px; margin: 8vh auto 0; }
.auth-card { background: var(--surface); border-radius: var(--radius); padding: 32px; box-shadow: var(--shadow); }
.auth-card h1 { font-size: 1.5em; margin-bottom: 6px; }
.auth-card .sub { color: var(--ink-soft); font-size: .9em; margin-bottom: 22px; }

/* ---------- error pages ---------- */
.error-page { max-width: 560px; margin: 12vh auto 0; text-align: center; }
.error-page .code { font-size: 5em; font-weight: 700; color: #fff; opacity: .55; line-height: 1; }
.error-page .card { margin-top: 18px; text-align: left; }

/* ---------- responsive ---------- */
@media (max-width: 768px) {
	body { padding: 12px; }
	.grid { grid-template-columns: 1fr; }
	.form-row, .form-row-3 { grid-template-columns: 1fr; }
	.header { flex-direction: column; align-items: stretch; }
	.header h1 { font-size: 1.45em; }
	.actions { flex-direction: column; }
	.actions .btn, .actions form, .actions form .btn { width: 100%; text-align: center; }
	.readings { grid-template-columns: repeat(2, 1fr); }
}

/* Printed pages are usually a maintenance record carried to the pond, so
   drop the gradient and the controls and keep the readings legible. */
@media print {
	body { background: #fff; padding: 0; color: #000; }
	.actions, .footer, .btn, form { display: none !important; }
	.card, .device-card { box-shadow: none; border: 1px solid #ccc; break-inside: avoid; }
}
