/* ============================================================
   SmartFarm Web App — CSS
   Mobile-first, responsive, dark theme
   ============================================================ */

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

:root {
    --green:      #2ecc71;
    --green-d:    #27ae60;
    --blue:       #3498db;
    --orange:     #e67e22;
    --red:        #e74c3c;
    --yellow:     #f1c40f;
    --bg:         #0d1520;
    --bg-card:    #162030;
    --bg-card2:   #1a2840;
    --text:       #e8edf2;
    --text-muted: #7f8ea0;
    --border:     #243040;
    --radius:     14px;
    --font:       'Sarabun', 'Noto Sans Thai', sans-serif;
    --bar-h:      56px;
    --nav-h:      60px;
}

html, body { background: var(--bg); color: var(--text); font-family: var(--font); }

/* ============================================================
   Layout
   ============================================================ */
.web-layout {
    min-height: 100vh;
    display: flex; flex-direction: column;
    max-width: 480px; margin: 0 auto;
    position: relative;
}

/* App Bar */
.app-bar {
    position: sticky; top: 0; z-index: 100;
    background: rgba(13,21,32,0.92);
    backdrop-filter: blur(10px);
    border-bottom: 1px solid var(--border);
    height: var(--bar-h);
}
.app-bar-inner {
    display: flex; align-items: center;
    justify-content: space-between;
    height: 100%; padding: 0 16px;
}
.app-bar-left  { display: flex; align-items: center; gap: 8px; }
.app-bar-logo  { font-size: 22px; }
.app-bar-title { font-size: 18px; font-weight: 700; color: var(--green); }
.app-bar-right { display: flex; align-items: center; gap: 10px; }
.app-bar-user  { font-size: 13px; color: var(--text-muted); }

.btn-icon {
    background: transparent; border: none;
    color: var(--text-muted); font-size: 18px;
    cursor: pointer; padding: 6px;
    border-radius: 8px; line-height: 1;
    transition: color 0.2s;
}
.btn-icon:hover { color: var(--red); }

/* Main */
.web-main { flex: 1; padding-bottom: calc(var(--nav-h) + 8px); }

/* Bottom Navigation */
.bottom-nav {
    position: fixed; bottom: 0; left: 50%;
    transform: translateX(-50%);
    width: 100%; max-width: 480px;
    height: var(--nav-h);
    background: var(--bg-card);
    border-top: 1px solid var(--border);
    display: flex; z-index: 100;
}
.bottom-nav-item {
    flex: 1; display: flex; flex-direction: column;
    align-items: center; justify-content: center;
    gap: 2px; text-decoration: none;
    color: var(--text-muted); font-size: 12px;
    transition: color 0.15s;
}
.bottom-nav-item.active  { color: var(--green); }
.bottom-nav-item:hover   { color: var(--text); }
.nav-icon  { font-size: 20px; line-height: 1; }
.nav-label { font-size: 11px; }

/* ============================================================
   Page Common
   ============================================================ */
.page-wrap { padding: 12px 12px 0; }

.page-header {
    display: flex; align-items: center;
    justify-content: space-between;
    margin-bottom: 14px; gap: 8px; flex-wrap: wrap;
}
.page-title { font-size: 18px; font-weight: 700; }

.page-loading {
    display: flex; justify-content: center;
    align-items: center; padding: 60px;
}
.loader {
    width: 36px; height: 36px; border-radius: 50%;
    border: 3px solid var(--border);
    border-top-color: var(--green);
    animation: spin 0.8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

.empty-state {
    display: flex; flex-direction: column;
    align-items: center; gap: 12px;
    padding: 48px 24px; color: var(--text-muted);
    text-align: center;
}
.empty-icon { font-size: 48px; }
.btn-retry {
    padding: 10px 24px; background: var(--bg-card2);
    border: 1px solid var(--border); border-radius: 8px;
    color: var(--text); text-decoration: none; font-size: 14px;
    cursor: pointer;
}

/* Range Tabs */
.range-tabs { display: flex; gap: 4px; }
.range-tab {
    padding: 6px 12px; border-radius: 20px;
    border: 1px solid var(--border);
    background: transparent; color: var(--text-muted);
    font-size: 12px; cursor: pointer; font-family: var(--font);
    transition: all 0.15s;
}
.range-tab.active {
    background: var(--green); border-color: var(--green);
    color: #fff;
}

/* ============================================================
   Login
   ============================================================ */
.login-wrap {
    min-height: 100vh; display: flex;
    align-items: center; justify-content: center;
    padding: 24px; background: var(--bg);
}
.login-card {
    width: 100%; max-width: 360px;
    background: var(--bg-card);
    border: 1px solid var(--border);
    border-radius: var(--radius); padding: 32px 24px;
}
.login-hero      { text-align: center; margin-bottom: 28px; }
.login-icon-wrap { margin-bottom: 12px; }
.login-icon      { font-size: 52px; }
.login-title     { font-size: 26px; font-weight: 700; color: var(--green); }
.login-desc      { font-size: 13px; color: var(--text-muted); margin-top: 4px; }

.field { margin-bottom: 16px; }
.field-label {
    display: block; font-size: 13px;
    color: var(--text-muted); margin-bottom: 6px;
}
.field-input {
    width: 100%; padding: 12px 14px;
    background: var(--bg); border: 1px solid var(--border);
    border-radius: 10px; color: var(--text);
    font-size: 15px; font-family: var(--font); outline: none;
}
.field-input:focus { border-color: var(--green); }
.field-password   { position: relative; }
.field-password .field-input { padding-right: 44px; }
.btn-eye {
    position: absolute; right: 12px; top: 50%;
    transform: translateY(-50%);
    background: transparent; border: none;
    cursor: pointer; font-size: 16px; padding: 4px;
}
.login-error {
    background: rgba(231,76,60,0.1);
    border: 1px solid var(--red);
    color: var(--red); border-radius: 8px;
    padding: 10px 12px; font-size: 13px;
    margin-bottom: 12px;
}
.btn-login {
    width: 100%; padding: 14px;
    background: var(--green); color: #fff;
    border: none; border-radius: 10px;
    font-size: 16px; font-weight: 600;
    font-family: var(--font);
    cursor: pointer; display: flex;
    align-items: center; justify-content: center; gap: 8px;
    transition: background 0.2s;
}
.btn-login:hover    { background: var(--green-d); }
.btn-login:disabled { opacity: 0.6; cursor: not-allowed; }
.login-footer {
    display: flex; align-items: center; gap: 6px;
    font-size: 12px; color: var(--text-muted);
    margin-top: 20px; justify-content: center;
}
.dot { width: 7px; height: 7px; border-radius: 50%; }
.dot-green { background: var(--green); }
.dot-red   { background: var(--red); }

/* ============================================================
   Dashboard
   ============================================================ */
.device-selector {
    display: flex; gap: 8px; overflow-x: auto;
    padding-bottom: 4px; margin-bottom: 12px;
}
.device-chip {
    padding: 6px 14px; border-radius: 20px;
    border: 1px solid var(--border);
    background: var(--bg-card); color: var(--text-muted);
    font-size: 13px; cursor: pointer; white-space: nowrap;
    display: flex; align-items: center; gap: 6px;
    font-family: var(--font);
    transition: all 0.15s;
}
.device-chip.active { border-color: var(--green); color: var(--green); }

.device-header {
    display: flex; align-items: center;
    justify-content: space-between;
    margin-bottom: 12px;
}
.device-name   { font-size: 16px; font-weight: 700; }
.device-status {
    display: flex; align-items: center; gap: 6px;
    font-size: 12px; padding: 4px 10px;
    border-radius: 20px;
}
.device-status.online  { background: rgba(46,204,113,0.15); color: var(--green); }
.device-status.offline { background: rgba(127,142,160,0.1); color: var(--text-muted); }

.pulse-dot {
    width: 7px; height: 7px; border-radius: 50%;
    background: currentColor;
}
.device-status.online .pulse-dot {
    animation: pulse 2s infinite;
}
@keyframes pulse {
    0%,100% { opacity: 1; }
    50%      { opacity: 0.3; }
}

.device-mode {
    font-size: 12px; padding: 4px 10px;
    border-radius: 20px; font-weight: 600;
}
.mode-auto      { background: rgba(46,204,113,0.15);  color: var(--green); }
.mode-manual    { background: rgba(241,196,15,0.15);  color: var(--yellow); }
.mode-emergency { background: rgba(231,76,60,0.15);   color: var(--red); }
.mode-unknown   { background: rgba(127,142,160,0.1);  color: var(--text-muted); }

/* Metrics Grid */
.metrics-grid {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 10px; margin-bottom: 12px;
}
.metric-card {
    background: var(--bg-card);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 14px;
}
.card-featured { grid-column: 1 / -1; border-color: rgba(52,152,219,0.4); }
.card-warn     { border-color: rgba(231,76,60,0.4); }
.card-pump-on  { border-color: rgba(52,152,219,0.4);
                 background: rgba(52,152,219,0.06); }
.card-channel  {}

.metric-header {
    display: flex; align-items: center; gap: 6px;
    margin-bottom: 8px;
}
.metric-icon { font-size: 16px; }
.metric-name { font-size: 12px; color: var(--text-muted); }
.metric-big  {
    font-size: 32px; font-weight: 700;
    color: var(--text); line-height: 1;
}
.metric-mid  { font-size: 22px; font-weight: 700; color: var(--green); }
.metric-warn { font-size: 12px; color: var(--red); margin-top: 4px; }
.pump-text   { color: var(--blue); }

/* Progress bar */
.progress-wrap { margin-top: 10px; }
.progress-track {
    height: 8px; background: var(--border);
    border-radius: 4px; overflow: hidden;
    margin-bottom: 4px;
}
.progress-fill {
    height: 100%; border-radius: 4px;
    transition: width 0.5s;
}
.moisture-fill { background: var(--blue); }
.progress-markers {
    display: flex; justify-content: space-between;
    font-size: 10px; color: var(--text-muted);
}

/* Mini bar */
.mini-track {
    height: 5px; background: var(--border);
    border-radius: 3px; overflow: hidden;
    margin-top: 8px;
}
.mini-fill { height: 100%; border-radius: 3px; transition: width 0.5s; }
.fill-dry  { background: var(--orange); }
.fill-ok   { background: var(--green); }
.fill-wet  { background: var(--blue); }

.device-dot { width: 7px; height: 7px; border-radius: 50%; }
.device-dot.online  { background: var(--green); }
.device-dot.offline { background: var(--text-muted); }

.last-updated {
    display: flex; align-items: center; gap: 8px;
    font-size: 12px; color: var(--text-muted);
    padding: 8px 0 14px;
}
.btn-refresh {
    background: transparent; border: none;
    cursor: pointer; font-size: 14px; padding: 2px;
}

/* ============================================================
   History
   ============================================================ */
.chart-card {
    background: var(--bg-card);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 14px; margin-bottom: 10px;
}
.chart-title  { font-size: 13px; color: var(--text-muted); margin-bottom: 8px; }
.chart-wrap   { width: 100%; overflow: hidden; }
.line-chart   { width: 100%; height: 140px; display: block; }
.chart-legend {
    display: flex; gap: 12px; margin-top: 8px; flex-wrap: wrap;
}
.legend-item  { display: flex; align-items: center; gap: 5px; font-size: 11px; color: var(--text-muted); }
.legend-line  { width: 14px; height: 2px; border-radius: 1px; }

.stats-row {
    display: grid; grid-template-columns: repeat(4, 1fr);
    gap: 8px; margin-bottom: 10px;
}
.stat-card {
    background: var(--bg-card);
    border: 1px solid var(--border);
    border-radius: 10px; padding: 10px 8px;
    text-align: center;
}
.stat-label { font-size: 10px; color: var(--text-muted); }
.stat-val   { font-size: 16px; font-weight: 700; margin-top: 2px; }

.table-card {
    background: var(--bg-card);
    border: 1px solid var(--border);
    border-radius: var(--radius); overflow: hidden;
    margin-bottom: 14px;
}
.table-title { padding: 12px 14px; font-size: 13px; color: var(--text-muted); border-bottom: 1px solid var(--border); }
.table-scroll { overflow-x: auto; }
.data-table { width: 100%; border-collapse: collapse; font-size: 12px; }
.data-table th {
    padding: 8px 10px; text-align: left;
    color: var(--text-muted); background: var(--bg-card2);
    white-space: nowrap;
}
.data-table td { padding: 8px 10px; border-top: 1px solid var(--border); white-space: nowrap; }
.row-pump td  { background: rgba(52,152,219,0.06); }
.badge {
    padding: 2px 8px; border-radius: 10px;
    font-size: 11px; font-weight: 600;
}
.mode-auto      { background: rgba(46,204,113,0.15); color: var(--green); }
.mode-manual    { background: rgba(241,196,15,0.15);  color: var(--yellow); }
.mode-emergency { background: rgba(231,76,60,0.15);   color: var(--red); }

/* ============================================================
   Settings
   ============================================================ */
.settings-list  { display: flex; flex-direction: column; gap: 12px; padding-bottom: 16px; }
.settings-group {
    background: var(--bg-card);
    border: 1px solid var(--border);
    border-radius: var(--radius); overflow: hidden;
}
.group-title {
    padding: 12px 16px; font-size: 14px; font-weight: 600;
    color: var(--green); background: rgba(46,204,113,0.06);
    border-bottom: 1px solid var(--border);
}
.setting-row {
    display: flex; align-items: center;
    justify-content: space-between; gap: 12px;
    padding: 14px 16px;
    border-bottom: 1px solid var(--border);
}
.setting-row:last-child { border-bottom: none; }
.setting-info  { flex: 1; min-width: 0; }
.setting-name  { font-size: 14px; }
.setting-desc  { font-size: 11px; color: var(--text-muted); margin-top: 2px; }
.setting-control { display: flex; align-items: center; gap: 6px; flex-shrink: 0; }
.setting-input {
    width: 80px; padding: 8px 10px;
    background: var(--bg); border: 1px solid var(--border);
    border-radius: 8px; color: var(--text);
    font-size: 15px; font-family: var(--font);
    text-align: center; outline: none;
}
.setting-input:focus { border-color: var(--green); }
.setting-time { width: 100px; }
.setting-unit { font-size: 12px; color: var(--text-muted); }

.btn-save {
    width: 100%; padding: 14px;
    background: var(--green); color: #fff;
    border: none; border-radius: 10px;
    font-size: 16px; font-weight: 600;
    font-family: var(--font); cursor: pointer;
    display: flex; align-items: center;
    justify-content: center; gap: 8px;
    transition: background 0.2s;
}
.btn-save:hover    { background: var(--green-d); }
.btn-save:disabled { opacity: 0.6; cursor: not-allowed; }

.toast {
    padding: 12px 16px; border-radius: 10px;
    font-size: 13px;
}
.toast-ok  { background: rgba(46,204,113,0.1); color: var(--green); border: 1px solid var(--green); }
.toast-err { background: rgba(231,76,60,0.1);  color: var(--red);   border: 1px solid var(--red); }

/* ============================================================
   Irrigation Timeline
   ============================================================ */
.irr-summary {
    display: grid; grid-template-columns: repeat(3, 1fr);
    gap: 8px; margin-bottom: 12px;
}
.irr-stat {
    background: var(--bg-card);
    border: 1px solid var(--border);
    border-radius: 12px; padding: 14px 10px;
    text-align: center;
}
.irr-stat-num   { font-size: 22px; font-weight: 700; color: var(--blue); }
.irr-stat-label { font-size: 11px; color: var(--text-muted); margin-top: 2px; }

.timeline-card {
    background: var(--bg-card);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 8px 16px; margin-bottom: 16px;
}
.timeline-item {
    display: flex; gap: 12px;
    padding: 12px 0;
    border-bottom: 1px solid var(--border);
}
.timeline-item:last-child { border-bottom: none; }
.timeline-dot {
    width: 10px; height: 10px; border-radius: 50%;
    flex-shrink: 0; margin-top: 4px;
}
.dot-on  { background: var(--blue); box-shadow: 0 0 6px rgba(52,152,219,0.5); }
.dot-off { background: var(--border); }
.timeline-body   { flex: 1; min-width: 0; }
.timeline-main   { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.timeline-action { font-size: 14px; font-weight: 600; }
.action-on       { color: var(--blue); }
.action-off      { color: var(--text-muted); }
.timeline-trigger{ font-size: 12px; color: var(--text-muted); }
.timeline-meta   { font-size: 11px; color: var(--text-muted); margin-top: 4px; display: flex; gap: 6px; flex-wrap: wrap; }

/* ============================================================
   Shared utilities
   ============================================================ */
.spin-sm {
    width: 16px; height: 16px; border-radius: 50%;
    border: 2px solid rgba(255,255,255,0.3);
    border-top-color: #fff;
    animation: spin 0.7s linear infinite;
}

/* Desktop: center card */
@media (min-width: 520px) {
    .web-layout { box-shadow: 0 0 40px rgba(0,0,0,0.5); }
}
