/* ── Reset & base ── */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
body { font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
       font-size: 15px; background: #f3f4f6; color: #111827; min-height: 100vh; }
a { color: #2563eb; text-decoration: none; }
a:hover { text-decoration: underline; }

/* ── Navbar ── */
.navbar { display: flex; align-items: center; gap: 1.5rem; padding: 0 1.5rem;
          height: 52px; background: #1e293b; color: #f1f5f9; position: sticky; top: 0; z-index: 100; }
.nav-brand { font-weight: 700; font-size: 1.1rem; color: #f1f5f9; letter-spacing: -0.3px; }
.nav-links { display: flex; gap: 1rem; }
.nav-links a { color: #cbd5e1; font-size: 0.9rem; }
.nav-links a:hover { color: #f1f5f9; text-decoration: none; }
.nav-user { margin-left: auto; display: flex; align-items: center; gap: 0.75rem; font-size: 0.85rem; }
.nav-domain { color: #94a3b8; }
.nav-email { color: #e2e8f0; }
.btn-logout { background: #334155; color: #cbd5e1; padding: 4px 10px;
              border-radius: 4px; font-size: 0.8rem; }
.btn-logout:hover { background: #475569; color: #f1f5f9; text-decoration: none; }

/* ── Main layout ── */
.main-content { padding: 1.5rem; max-width: 1400px; margin: 0 auto; }
.main-full { max-width: none; padding: 0; }

/* ── Login ── */
.login-body { background: #1e293b; display: flex; align-items: center; justify-content: center; min-height: 100vh; }
.login-card { background: #fff; border-radius: 12px; padding: 2.5rem; width: 100%; max-width: 400px;
              box-shadow: 0 20px 60px rgba(0,0,0,0.3); }
.login-logo { text-align: center; margin-bottom: 2rem; }
.login-logo h1 { font-size: 1.5rem; margin-top: 0.5rem; color: #1e293b; }
.login-logo p { color: #64748b; font-size: 0.9rem; margin-top: 0.25rem; }
.login-form label { display: block; font-size: 0.85rem; font-weight: 500; color: #374151; margin-bottom: 4px; margin-top: 1rem; }
.login-form input { width: 100%; padding: 0.6rem 0.75rem; border: 1px solid #d1d5db;
                    border-radius: 6px; font-size: 0.95rem; outline: none; }
.login-form input:focus { border-color: #2563eb; box-shadow: 0 0 0 3px rgba(37,99,235,0.15); }
.login-form .btn-primary { width: 100%; margin-top: 1.5rem; padding: 0.7rem; font-size: 1rem; }

/* ── Alerts ── */
.alert { padding: 0.6rem 0.9rem; border-radius: 6px; margin-bottom: 1rem; font-size: 0.9rem; }
.alert-error { background: #fef2f2; border: 1px solid #fca5a5; color: #991b1b; }

/* ── Buttons ── */
.btn-primary { background: #2563eb; color: #fff; border: none; padding: 0.5rem 1.1rem;
               border-radius: 6px; cursor: pointer; font-size: 0.9rem; font-weight: 500;
               display: inline-block; }
.btn-primary:hover { background: #1d4ed8; text-decoration: none; color: #fff; }
.btn-secondary { background: #e2e8f0; color: #334155; border: none; padding: 0.5rem 1rem;
                 border-radius: 6px; cursor: pointer; font-size: 0.9rem; display: inline-block; }
.btn-secondary:hover { background: #cbd5e1; text-decoration: none; color: #1e293b; }
.btn-danger { background: #ef4444; color: #fff; border: none; padding: 0.5rem 1rem;
              border-radius: 6px; cursor: pointer; font-size: 0.9rem; display: inline-block; }
.btn-danger:hover { background: #dc2626; }
.btn-sm { padding: 0.3rem 0.7rem; font-size: 0.82rem; }
.mt-1 { margin-top: 0.5rem; }
.mt-2 { margin-top: 1rem; }
.mb-2 { margin-bottom: 1rem; }

/* ── Cards ── */
.card { background: #fff; border-radius: 8px; box-shadow: 0 1px 3px rgba(0,0,0,0.07); overflow: hidden; }
.card-header { display: flex; align-items: center; justify-content: space-between;
               padding: 0.85rem 1.1rem; border-bottom: 1px solid #e5e7eb; }
.card-header h3 { font-size: 0.95rem; font-weight: 600; }

/* ── Page header ── */
.page-header { display: flex; align-items: center; gap: 1rem; margin-bottom: 1.25rem; }
.page-header h2 { font-size: 1.3rem; font-weight: 700; }

/* ── Stats grid ── */
.stats-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
              gap: 1rem; margin-bottom: 1.25rem; }
.stat-card { background: #fff; border-radius: 8px; padding: 1.25rem; text-align: center;
             box-shadow: 0 1px 3px rgba(0,0,0,0.07); }
.stat-value { font-size: 2rem; font-weight: 700; color: #2563eb; }
.stat-label { font-size: 0.85rem; color: #64748b; margin-top: 0.25rem; }

/* ── Data table ── */
.data-table { width: 100%; border-collapse: collapse; }
.data-table th, .data-table td { padding: 0.65rem 1.1rem; text-align: left;
                                   border-bottom: 1px solid #f1f5f9; font-size: 0.9rem; }
.data-table th { background: #f8fafc; font-weight: 600; color: #374151; font-size: 0.82rem; text-transform: uppercase; letter-spacing: 0.05em; }
.data-table tr:hover td { background: #f8fafc; }
.actions { display: flex; gap: 0.4rem; }
.empty { color: #9ca3af; padding: 1.25rem; text-align: center; font-style: italic; }

/* ── Badge ── */
.badge { font-size: 0.75rem; padding: 2px 7px; border-radius: 99px; font-weight: 500; }
.badge-admin { background: #dbeafe; color: #1d4ed8; }

/* ── Inline form ── */
.inline-form { display: flex; align-items: center; gap: 0.6rem; padding: 0.75rem 1.1rem; flex-wrap: wrap; }
.inline-form input[type=text], .inline-form input[type=email], .inline-form input[type=password] {
  padding: 0.4rem 0.7rem; border: 1px solid #d1d5db; border-radius: 6px; font-size: 0.9rem; }
.checkbox-label { display: flex; align-items: center; gap: 0.35rem; font-size: 0.9rem; cursor: pointer; }
.edit-row td { background: #f8fafc; }

/* ── Page list ── */
.page-list { display: flex; flex-direction: column; }
.page-item { display: flex; align-items: center; justify-content: space-between;
             padding: 0.75rem 1.1rem; border-bottom: 1px solid #f1f5f9; }
.page-item:last-child { border-bottom: none; }
.page-name { font-weight: 500; font-size: 1rem; }
.page-actions { display: flex; gap: 0.5rem; }

/* ── HMI Grid ── */
.grid-canvas { display: grid; grid-template-columns: repeat(12, 1fr);
               grid-auto-rows: 80px; gap: 10px; padding: 0.5rem; }

/* ── Widget tiles ── */
.widget-tile { background: #fff; border-radius: 8px; padding: 0.85rem;
               box-shadow: 0 1px 3px rgba(0,0,0,0.08); min-height: 80px;
               display: flex; flex-direction: column; justify-content: center;
               position: relative; overflow: hidden; }
.widget-tile.widget-text  { border-left: 4px solid #6b7280; }
.widget-tile.widget-numeric { border-left: 4px solid #2563eb; }
.widget-tile.widget-trend   { border-left: 4px solid #16a34a; }
.widget-tile.widget-meter   { border-left: 4px solid #d97706; }
.widget-tile.selected { outline: 2px solid #2563eb; }
.widget-tile.layout-overlap { outline: 2px solid #ef4444; background: #fef2f2; z-index: 10; position: relative; }

.widget-label { font-size: 0.78rem; font-weight: 600; color: #6b7280; text-transform: uppercase;
                letter-spacing: 0.06em; margin-bottom: 0.35rem; }
.widget-value { display: flex; align-items: baseline; gap: 0.4rem; }
.value-display { font-size: 1.9rem; font-weight: 700; color: #1e293b; line-height: 1; }
.value-units   { font-size: 0.9rem; color: #64748b; }
.widget-text-content { font-size: 1rem; color: #374151; }

/* Trend — canvas fills a positioned wrapper so Chart.js has a fixed box */
.widget-tile.widget-trend { justify-content: flex-start; }
.trend-wrapper { position: relative; flex: 1; min-height: 0; width: 100%; }
.trend-canvas { position: absolute; top: 0; left: 0; width: 100% !important; height: 100% !important; }

/* Meter */
.meter-container { display: flex; flex-direction: column; align-items: center; }
.meter-svg { width: 100%; max-width: 120px; }
.meter-value { font-size: 0.9rem; font-weight: 600; color: #1e293b; text-align: center; margin-top: 0.25rem; }
.meter-value .value-display { font-size: 1.1rem; }

/* Tank */
.widget-tile.widget-tank { border-left: 4px solid #0ea5e9; justify-content: flex-start; }
.tank-container { display: flex; flex-direction: column; align-items: center; flex: 1; min-height: 0; width: 100%; }
.tank-wrapper { flex: 1; min-height: 0; display: flex; justify-content: center; align-items: stretch; width: 100%; }
.tank-svg { height: 100%; width: auto; max-width: 80px; }
.tank-value { font-size: 0.85rem; font-weight: 600; color: #1e293b; text-align: center; margin-top: 0.25rem; }
.tank-value .value-display { font-size: 1rem; }

/* ── Runtime full-width padding ── */
.main-full .runtime-header { padding: 1rem 1.5rem 0; }
.main-full .grid-canvas    { padding: 1rem; }

/* ── Editor layout ── */
.editor-layout { display: flex; gap: 0; height: calc(100vh - 52px); overflow: hidden; }
.editor-sidebar { width: 200px; min-width: 200px; background: #1e293b; color: #e2e8f0;
                  overflow-y: auto; display: flex; flex-direction: column; }
.editor-props   { width: 260px; min-width: 260px; background: #1e293b; color: #e2e8f0;
                  overflow-y: auto; }
.sidebar-section { padding: 1rem; border-bottom: 1px solid #334155; }
.sidebar-section h3 { font-size: 0.95rem; margin-bottom: 0.5rem; }
.sidebar-section h4 { font-size: 0.82rem; text-transform: uppercase; letter-spacing: 0.06em;
                      color: #94a3b8; margin-bottom: 0.6rem; }
.editor-main { flex: 1; overflow-y: auto; padding: 1rem; background: #f1f5f9; }

/* Grid rulers */
.grid-ruler-wrap { display: grid; grid-template-columns: 28px 1fr; grid-template-rows: 20px 1fr; }
.grid-ruler-col { display: grid; grid-template-columns: repeat(12, 1fr); gap: 10px;
                  padding: 0 0.5rem; grid-column: 2; }
.grid-ruler-col span { text-align: center; font-size: 0.65rem; font-weight: 600;
                       color: #94a3b8; line-height: 20px; }
.grid-ruler-row { grid-column: 1; grid-row: 2; display: flex; flex-direction: column;
                  padding: 0.5rem 0; gap: 0; }
.grid-ruler-row span { height: 90px; display: flex; align-items: center; justify-content: center;
                       font-size: 0.65rem; font-weight: 600; color: #94a3b8; padding-top: 2px; }
.grid-canvas-wrap { grid-column: 2; grid-row: 2; }

/* Palette */
.widget-palette { display: flex; flex-direction: column; gap: 0.4rem; }
.palette-section-label { font-size: 0.7rem; font-weight: 700; text-transform: uppercase;
                         letter-spacing: 0.08em; color: #64748b; padding: 0.4rem 0 0.1rem; }
.palette-btn { background: #334155; color: #e2e8f0; border: none; border-radius: 6px;
               padding: 0.55rem 0.8rem; text-align: left; cursor: pointer; font-size: 0.88rem;
               display: flex; align-items: center; gap: 0.5rem; }
.palette-btn:hover { background: #475569; }
.palette-icon { width: 22px; height: 22px; background: #2563eb; border-radius: 4px;
                display: flex; align-items: center; justify-content: center;
                font-size: 0.75rem; font-weight: 700; color: #fff; flex-shrink: 0; }
.palette-icon svg { display: block; }

/* Props form */
#props-form label { display: block; font-size: 0.82rem; color: #94a3b8; margin-bottom: 2px; margin-top: 0.6rem; }
#props-form input[type=text], #props-form input[type=number], #props-form select {
  width: 100%; background: #0f172a; color: #e2e8f0; border: 1px solid #334155;
  border-radius: 5px; padding: 0.4rem 0.5rem; font-size: 0.88rem; }
#props-form input:focus, #props-form select:focus { outline: none; border-color: #2563eb; }
.props-section-label { font-size: 0.7rem; font-weight: 700; text-transform: uppercase;
  letter-spacing: 0.05em; color: #475569; margin-top: 1rem; margin-bottom: 0.1rem; }
.props-layout-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 0 0.5rem; }
.props-layout-grid label { display: block; }
.props-layout-grid input[type=range] { width: 100%; margin-top: 2px; }
.layout-val { float: right; font-weight: 600; color: #e2e8f0; min-width: 1.4rem; text-align: center;
              border-bottom: 1px dashed #475569; cursor: text; border-radius: 2px; }
.layout-val:focus { outline: none; background: #0f172a; border-bottom-color: #2563eb; }
.payload-preview { display: none; }

/* Editor widget tile interaction */
.editor-main .widget-tile { cursor: pointer; }
.editor-main .widget-tile .widget-label { font-size: 0.72rem; }
.editor-main .widget-tile .widget-type-badge {
  position: absolute; top: 6px; right: 6px; font-size: 0.68rem; background: #e2e8f0;
  color: #475569; padding: 1px 5px; border-radius: 3px; font-weight: 600; }
.grid-cell-placeholder { background: #e2e8f0; border: 2px dashed #cbd5e1;
                          border-radius: 8px; cursor: pointer; min-height: 80px;
                          display: flex; align-items: center; justify-content: center;
                          font-size: 1.5rem; color: #94a3b8; }
.grid-cell-placeholder:hover { background: #dbeafe; border-color: #93c5fd; }
.widget-ghost { background: rgba(37,99,235,0.12); border: 2px dashed #2563eb;
                border-radius: 8px; pointer-events: none; transition: background 0.15s; }
.widget-ghost.overlap { background: rgba(239,68,68,0.12); border-color: #ef4444; }

/* ── Add widget dialog ── */
.widget-dialog { border: none; border-radius: 10px; padding: 1.5rem;
                 box-shadow: 0 20px 60px rgba(0,0,0,0.25); min-width: 340px;
                 position: fixed; top: 50%; left: 50%; transform: translate(-50%, -50%); margin: 0; }
.widget-dialog::backdrop { background: rgba(0,0,0,0.4); }
.widget-dialog h3 { margin-bottom: 1rem; font-size: 1.05rem; }
.form-row { display: flex; gap: 1rem; margin-bottom: 0.75rem; }
.form-row label { flex: 1; font-size: 0.85rem; display: flex; flex-direction: column; gap: 4px; }
.form-row input { padding: 0.4rem 0.6rem; border: 1px solid #d1d5db; border-radius: 5px; font-size: 0.9rem; }
#add-config-fields label { display: block; font-size: 0.85rem; margin-bottom: 4px; margin-top: 0.6rem; }
#add-config-fields input, #add-config-fields select { width: 100%; padding: 0.4rem 0.6rem;
  border: 1px solid #d1d5db; border-radius: 5px; font-size: 0.9rem; }
.dialog-actions { display: flex; gap: 0.5rem; margin-top: 1.25rem; justify-content: flex-end; }

/* ── Runtime header ── */
.runtime-header { display: flex; align-items: center; gap: 0.75rem; margin-bottom: 1rem; }
.runtime-header h2 { font-size: 1.25rem; font-weight: 700; flex: 1; }
.ws-status { font-size: 1.1rem; color: #ef4444; }
.ws-status.connected { color: #16a34a; }

/* ── Indicator light ── */
.widget-tile.widget-indicator { display: flex; flex-direction: column; align-items: center; justify-content: center; }
.indicator-container { display: flex; align-items: center; justify-content: center; flex: 1; }
.indicator-lamp { width: 44px; height: 44px; border-radius: 50%; background: #374151;
  border: 3px solid #4b5563; position: relative;
  transition: background 0.2s, box-shadow 0.2s, border-color 0.2s; }
.indicator-lamp.on { background: #22c55e; border-color: #16a34a;
  box-shadow: 0 0 10px #22c55e, 0 0 22px rgba(34,197,94,0.45); }
.indicator-glare { position: absolute; top: 18%; left: 18%; width: 36%; height: 36%;
  border-radius: 50%; background: rgba(255,255,255,0.28); }

/* ── Valve ── */
.widget-tile.widget-valve { display: flex; flex-direction: column; align-items: center; justify-content: center; }
.valve-container { display: flex; flex-direction: column; align-items: center; justify-content: center; flex: 1; width: 100%; }
.valve-svg { width: 100%; max-width: 120px; height: auto; }
.valve-svg .valve-pipe { fill: #64748b; }
.valve-svg .valve-stem { stroke: #64748b; fill: none; stroke-width: 2; }
.valve-svg .valve-stem-body { fill: #64748b; }
.valve-svg .valve-body { stroke: #475569; stroke-width: 1.5; transition: fill 0.3s; }
.valve-svg.valve-closed .valve-body { fill: #ef4444; }
.valve-svg.valve-open .valve-body { fill: #22c55e; }
.valve-svg .valve-disc { stroke: #e2e8f0; stroke-width: 3; stroke-linecap: round;
  transition: transform 0.3s ease; transform-origin: 40px 35px; }
.valve-svg.valve-open .valve-disc { transform: rotate(90deg); }
.valve-label-state { font-size: 0.75rem; font-weight: 600; margin-top: 4px;
  color: #64748b; letter-spacing: 0.05em; }
.valve-svg.valve-open ~ .valve-label-state { color: #16a34a; }
.valve-svg.valve-closed ~ .valve-label-state { color: #dc2626; }

/* ── Button widget ── */
.widget-tile.widget-button { border-left: 4px solid #7c3aed; }
.button-container { display: flex; align-items: center; justify-content: center; flex: 1; }
.hmi-btn { border: none; border-radius: 8px; padding: 0.55rem 1.4rem; font-size: 1rem; font-weight: 700;
           cursor: pointer; transition: background 0.2s, box-shadow 0.2s; letter-spacing: 0.04em;
           min-width: 80px; }
.hmi-btn.hmi-btn-off { background: #374151; color: #9ca3af; }
.hmi-btn.hmi-btn-off:hover { background: #4b5563; color: #e2e8f0; }
.hmi-btn.hmi-btn-on  { background: #22c55e; color: #fff; box-shadow: 0 0 10px rgba(34,197,94,0.4); }
.hmi-btn.hmi-btn-on:hover  { background: #16a34a; }

/* ── Setpoint widget ── */
.widget-tile.widget-setpoint { border-left: 4px solid #f59e0b; }
.setpoint-container { display: flex; flex-direction: column; gap: 0.5rem; flex: 1; justify-content: center; }
.setpoint-current { display: flex; align-items: baseline; gap: 0.3rem; }
.setpoint-current .value-display { font-size: 1.4rem; font-weight: 700; color: #1e293b; }
.setpoint-input-row { display: flex; gap: 0.4rem; }
.setpoint-input { flex: 1; min-width: 0; padding: 0.35rem 0.5rem; border: 1px solid #d1d5db;
                  border-radius: 5px; font-size: 0.9rem; }
.setpoint-input:focus { outline: none; border-color: #2563eb; box-shadow: 0 0 0 2px rgba(37,99,235,0.2); }
.hmi-send-btn { background: #2563eb; color: #fff; border: none; border-radius: 5px;
                padding: 0.35rem 0.65rem; cursor: pointer; font-size: 0.85rem; font-weight: 600;
                white-space: nowrap; }
.hmi-send-btn:hover { background: #1d4ed8; }

/* ── Slider widget ── */
.widget-tile.widget-slider { border-left: 4px solid #ec4899; }
.slider-container { display: flex; flex-direction: column; gap: 0.4rem; flex: 1; justify-content: center; }
.slider-value { display: flex; align-items: baseline; gap: 0.3rem; }
.slider-value .value-display { font-size: 1.4rem; font-weight: 700; color: #1e293b; }
.hmi-slider { width: 100%; cursor: pointer; accent-color: #2563eb; }

/* ── Responsive ── */
@media (max-width: 768px) {
  .grid-canvas { grid-template-columns: repeat(4, 1fr); }
  .editor-layout { flex-direction: column; height: auto; }
  .editor-sidebar { width: 100%; min-width: 0; }
}
