/* Reset e stili base per TV 4K UHD & FullHD */
* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

:root {
  --bg-primary: #0a0e17;
  --bg-card: #111827;
  --border-color: #374151;
  --text-main: #f3f4f6;
  --text-muted: #9ca3af;
  --accent-blue: #3b82f6;
  --accent-cyan: #06b6d4;
  --accent-green: #10b981;
  --topbar-height: 48px;
}

html, body {
  width: 100vw;
  height: 100vh;
  overflow: hidden;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  background-color: var(--bg-primary);
  color: var(--text-main);
  user-select: none;
}

/* Header Top Bar */
.top-bar {
  height: var(--topbar-height);
  background: rgba(17, 24, 39, 0.95);
  backdrop-filter: blur(8px);
  border-bottom: 1px solid var(--border-color);
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 16px;
  position: relative;
  z-index: 100;
  transition: transform 0.3s ease, opacity 0.3s ease;
}

/* Auto-hide top bar in kiosk/fullscreen mode */
body.kiosk-mode .top-bar {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  opacity: 0;
  transform: translateY(-100%);
}

body.kiosk-mode .top-bar:hover,
body.kiosk-mode .top-bar.force-visible {
  opacity: 1;
  transform: translateY(0);
}

.brand {
  display: flex;
  align-items: center;
  gap: 12px;
}

.status-dot {
  width: 10px;
  height: 10px;
  background-color: var(--accent-green);
  border-radius: 50%;
  box-shadow: 0 0 8px var(--accent-green);
  animation: pulse 2s infinite;
}

.status-dot.wakelock-active {
  box-shadow: 0 0 10px #10b981, 0 0 15px #06b6d4;
}

@keyframes pulse {
  0% { transform: scale(0.95); opacity: 0.8; }
  50% { transform: scale(1.15); opacity: 1; }
  100% { transform: scale(0.95); opacity: 0.8; }
}

.brand-title {
  font-size: 15px;
  font-weight: 700;
  letter-spacing: 0.5px;
  color: #fff;
}

.current-time {
  font-family: "Courier New", monospace;
  font-size: 14px;
  color: var(--accent-cyan);
  font-weight: bold;
  background: rgba(6, 182, 212, 0.1);
  padding: 2px 8px;
  border-radius: 4px;
}

.controls {
  display: flex;
  align-items: center;
  gap: 14px;
}

.layout-selector, .refresh-controls, .zoom-controls {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 13px;
  color: var(--text-muted);
}

.btn-layout {
  background: #1e293b;
  color: var(--text-main);
  border: 1px solid var(--border-color);
  padding: 5px 10px;
  border-radius: 4px;
  font-size: 12px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.2s;
  white-space: nowrap;
}

.btn-layout:hover {
  background: #334155;
  border-color: var(--accent-blue);
}

.btn-layout.active {
  background: var(--accent-blue);
  color: #fff;
  border-color: var(--accent-blue);
  box-shadow: 0 0 10px rgba(59, 130, 246, 0.4);
}

.select-interval, .select-zoom {
  background: #1e293b;
  color: var(--text-main);
  border: 1px solid var(--border-color);
  padding: 4px 8px;
  border-radius: 4px;
  font-size: 12px;
  outline: none;
}

.btn-icon {
  background: #1e293b;
  color: var(--text-main);
  border: 1px solid var(--border-color);
  width: 32px;
  height: 32px;
  border-radius: 4px;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  font-size: 14px;
  transition: all 0.2s;
}

.btn-icon:hover {
  background: #334155;
  border-color: var(--accent-blue);
}

/* Griglia Wallboard */
.wallboard-container {
  width: 100vw;
  height: calc(100vh - var(--topbar-height));
  display: grid;
  gap: 6px;
  padding: 6px;
  transition: all 0.3s ease;
}

body.kiosk-mode .wallboard-container {
  height: 100vh;
}

/* ==========================================================================
   LAYOUTS CSS GRID
   ========================================================================== */

/* LAYOUT 1: 2x2 (4 Riquadri uguali) */
body.layout-grid-4 .wallboard-container {
  grid-template-columns: repeat(2, 1fr);
  grid-template-rows: repeat(2, 1fr);
}
body.layout-grid-4 #card-1 { grid-column: auto; grid-row: auto; }
body.layout-grid-4 #card-2 { grid-column: auto; grid-row: auto; }
body.layout-grid-4 #card-3 { grid-column: auto; grid-row: auto; }
body.layout-grid-4 #card-4 { grid-column: auto; grid-row: auto; }
body.layout-grid-4 #card-5,
body.layout-grid-4 #card-6,
body.layout-grid-4 #card-7,
body.layout-grid-4 #card-8,
body.layout-grid-4 #card-9,
body.layout-grid-4 #card-10 {
  display: none !important;
}

/* LAYOUT 2: 4+1 (2x2 sulla sinistra + 1 colonna verticale a destra) */
body.layout-grid-4-plus-1 .wallboard-container {
  grid-template-columns: 2fr 2fr 1.6fr;
  grid-template-rows: repeat(2, 1fr);
}
body.layout-grid-4-plus-1 #card-1 { grid-column: 1; grid-row: 1; }
body.layout-grid-4-plus-1 #card-2 { grid-column: 2; grid-row: 1; }
body.layout-grid-4-plus-1 #card-3 { grid-column: 1; grid-row: 2; }
body.layout-grid-4-plus-1 #card-4 { grid-column: 2; grid-row: 2; }
body.layout-grid-4-plus-1 #card-5 {
  grid-column: 3;
  grid-row: 1 / span 2;
  display: flex !important;
}
body.layout-grid-4-plus-1 #card-6,
body.layout-grid-4-plus-1 #card-7,
body.layout-grid-4-plus-1 #card-8,
body.layout-grid-4-plus-1 #card-9,
body.layout-grid-4-plus-1 #card-10 {
  display: none !important;
}

/* LAYOUT 3: 3x2 (6 Riquadri) */
body.layout-grid-6 .wallboard-container {
  grid-template-columns: repeat(3, 1fr);
  grid-template-rows: repeat(2, 1fr);
}
body.layout-grid-6 #card-1 { grid-column: auto; grid-row: auto; }
body.layout-grid-6 #card-2 { grid-column: auto; grid-row: auto; }
body.layout-grid-6 #card-3 { grid-column: auto; grid-row: auto; }
body.layout-grid-6 #card-4 { grid-column: auto; grid-row: auto; }
body.layout-grid-6 #card-5 { grid-column: auto; grid-row: auto; display: flex !important; }
body.layout-grid-6 #card-6 { grid-column: auto; grid-row: auto; display: flex !important; }
body.layout-grid-6 #card-7,
body.layout-grid-6 #card-8,
body.layout-grid-6 #card-9,
body.layout-grid-6 #card-10 {
  display: none !important;
}

/* LAYOUT 4: 10 Aree Combo (1 colonna 2x 1920x1080 + 2 colonne da 4 celle 960x540)
   In 4K UHD (3840x2160):
   - Colonna 1 (1920px = 2fr): 2 righe da 1080px (celle 1 e 2)
   - Colonna 2 (960px = 1fr): 4 righe da 540px (celle 3, 4, 5, 6)
   - Colonna 3 (960px = 1fr): 4 righe da 540px (celle 7, 8, 9, 10)
*/
body.layout-grid-10-combo .wallboard-container {
  grid-template-columns: 2fr 1fr 1fr;
  grid-template-rows: repeat(4, 1fr);
}

/* Cella 1 & 2: 1920x1080 (ciascuna occupa 2 righe in altezza) */
body.layout-grid-10-combo #card-1 {
  grid-column: 1;
  grid-row: 1 / span 2;
  display: flex !important;
}
body.layout-grid-10-combo #card-2 {
  grid-column: 1;
  grid-row: 3 / span 2;
  display: flex !important;
}

/* Colonna Centrale: 4 celle (960x540 cad) */
body.layout-grid-10-combo #card-3 {
  grid-column: 2;
  grid-row: 1;
  display: flex !important;
}
body.layout-grid-10-combo #card-4 {
  grid-column: 2;
  grid-row: 2;
  display: flex !important;
}
body.layout-grid-10-combo #card-5 {
  grid-column: 2;
  grid-row: 3;
  display: flex !important;
}
body.layout-grid-10-combo #card-6 {
  grid-column: 2;
  grid-row: 4;
  display: flex !important;
}

/* Colonna Destra: 4 celle (960x540 cad) */
body.layout-grid-10-combo #card-7 {
  grid-column: 3;
  grid-row: 1;
  display: flex !important;
}
body.layout-grid-10-combo #card-8 {
  grid-column: 3;
  grid-row: 2;
  display: flex !important;
}
body.layout-grid-10-combo #card-9 {
  grid-column: 3;
  grid-row: 3;
  display: flex !important;
}
body.layout-grid-10-combo #card-10 {
  grid-column: 3;
  grid-row: 4;
  display: flex !important;
}

/* Card / Frame: nessun bordo e niente barre di titolo per massimizzare l'area visiva */
.frame-card {
  background: var(--bg-card);
  border: 1px solid var(--border-color);
  border-radius: 4px;
  overflow: hidden;
  display: flex;
  flex-direction: column;
  position: relative;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.4);
}

/* La barra del titolo viene rimossa per dare il 100% dell'area all'iframe */
.frame-header {
  display: none !important;
}

/* Overlay pulsanti al passaggio del mouse (hover discreto se serve ricaricare o aprire) */
.frame-actions-hover {
  position: absolute;
  top: 6px;
  right: 6px;
  display: flex;
  gap: 4px;
  z-index: 20;
  opacity: 0;
  transition: opacity 0.2s ease;
  background: rgba(17, 24, 39, 0.85);
  backdrop-filter: blur(4px);
  padding: 3px 6px;
  border-radius: 4px;
  border: 1px solid rgba(255, 255, 255, 0.1);
}

.frame-card:hover .frame-actions-hover {
  opacity: 1;
}

.btn-mini-refresh, .btn-mini-link {
  background: transparent;
  color: var(--text-muted);
  border: none;
  font-size: 13px;
  cursor: pointer;
  text-decoration: none;
  line-height: 1;
  padding: 2px 4px;
  border-radius: 3px;
}

.btn-mini-refresh:hover, .btn-mini-link:hover {
  background: rgba(255, 255, 255, 0.15);
  color: #fff;
}

/* ==========================================================================
   VIEWPORT EMULATION & AUTO-ZOOM TRICK (100% ALTEZZA RIQUADRO)
   ========================================================================== */
.iframe-wrapper {
  flex: 1;
  position: relative;
  width: 100%;
  height: 100%;
  background: #000;
  overflow: hidden;
}

/* Modalità Scalata Full HD (Default): l'iframe pensa di essere a 1920x1080 */
.iframe-wrapper.emulate-fullhd iframe {
  position: absolute;
  top: 0;
  left: 0;
  width: 1920px;
  height: 1080px;
  transform-origin: 0 0;
  border: 0;
  display: block;
}

/* Modalità Responsiva nativa */
.iframe-wrapper.emulate-native iframe {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  transform: none !important;
  border: 0;
  display: block;
}

/* Loader Overlay */
.loader-overlay {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(17, 24, 39, 0.85);
  display: flex;
  align-items: center;
  justify-content: center;
  opacity: 1;
  transition: opacity 0.4s ease;
  pointer-events: none;
  z-index: 5;
}

.loader-overlay.hidden {
  opacity: 0;
}

.spinner {
  width: 28px;
  height: 28px;
  border: 3px solid rgba(59, 130, 246, 0.2);
  border-top-color: var(--accent-blue);
  border-radius: 50%;
  animation: spin 1s linear infinite;
}

@keyframes spin {
  to { transform: rotate(360deg); }
}

/* Elemento invisibile per Anti-Screensaver (video loop silenzioso) */
#nosleep-video {
  position: absolute;
  top: 0;
  left: 0;
  width: 1px;
  height: 1px;
  opacity: 0.01;
  pointer-events: none;
}

/* Modal */
.modal-backdrop {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.7);
  backdrop-filter: blur(4px);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
}

.modal-backdrop.hidden {
  display: none;
}

.modal-card {
  background: #1e293b;
  border: 1px solid var(--border-color);
  border-radius: 8px;
  width: 90%;
  max-width: 780px;
  max-height: 85vh;
  box-shadow: 0 10px 25px rgba(0,0,0,0.5);
  display: flex;
  flex-direction: column;
}

.modal-header {
  padding: 14px 20px;
  border-bottom: 1px solid var(--border-color);
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.modal-header h2 {
  font-size: 16px;
  color: #fff;
}

.btn-close {
  background: transparent;
  border: none;
  color: var(--text-muted);
  font-size: 20px;
  cursor: pointer;
}

.modal-body {
  padding: 16px 20px;
  overflow-y: auto;
}

.modal-note {
  font-size: 12px;
  color: var(--text-muted);
  margin-bottom: 14px;
}

.input-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
}

@media (max-width: 600px) {
  .input-grid {
    grid-template-columns: 1fr;
  }
}

.input-row {
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.input-row label {
  font-size: 11px;
  font-weight: 600;
  color: #93c5fd;
}

.input-row input {
  background: #0f172a;
  border: 1px solid var(--border-color);
  color: #fff;
  padding: 7px 10px;
  border-radius: 4px;
  font-size: 12px;
  outline: none;
}

.input-row input:focus {
  border-color: var(--accent-blue);
  box-shadow: 0 0 6px rgba(59, 130, 246, 0.4);
}

.modal-footer {
  padding: 12px 20px;
  border-top: 1px solid var(--border-color);
  display: flex;
  justify-content: flex-end;
  gap: 10px;
}

.btn-primary, .btn-secondary {
  padding: 8px 16px;
  border-radius: 4px;
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
  border: none;
}

.btn-primary {
  background: var(--accent-blue);
  color: #fff;
}

.btn-primary:hover {
  background: #2563eb;
}

.btn-secondary {
  background: #334155;
  color: #e2e8f0;
}

.btn-secondary:hover {
  background: #475569;
}
