/**
 * e-Sheets Styles
 * Digital Judge Sheets Modal Styles
 */

/* CSS Variables */
:root {
  --esheet-bg: #0b0f14;
  --esheet-muted: #2a2f36;
  --esheet-muted-2: #3a4048;
  --esheet-text: #eef2f7;
  --esheet-accent: #4f8cff;
  --esheet-red: #ef4444;
  --esheet-blue: #3b82f6;
  --esheet-safe-top: env(safe-area-inset-top);
  --esheet-safe-right: env(safe-area-inset-right);
  --esheet-safe-bottom: env(safe-area-inset-bottom);
  --esheet-safe-left: env(safe-area-inset-left);
  --esheet-overview-ar: 4 / 3;
}

/* Modal Container */
.esheet-modal {
  position: fixed;
  inset: 0;
  display: none;
  z-index: 9999;
}

.esheet-modal.open {
  display: block;
}

.esheet-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(10, 13, 18, 0.75);
  backdrop-filter: blur(4px);
}

/* Sheet Layout */
.esheet-sheet {
  position: absolute;
  inset: 0;
  display: grid;
  grid-template-rows: auto auto 1fr;
  min-height: 0;
  padding: calc(var(--esheet-safe-top, 0px)) calc(max(var(--esheet-safe-right, 0px), 0px)) calc(max(var(--esheet-safe-bottom, 0px), 8px)) calc(max(var(--esheet-safe-left, 0px), 0px));
  box-sizing: border-box;
  background: var(--esheet-bg);
}

/* Toolbar */
.esheet-toolbar {
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  align-items: center;
  gap: 10px;
  padding: 8px 10px;
  background: linear-gradient(180deg, rgba(22, 26, 32, 0.95), rgba(18, 22, 28, 0.9));
  border-bottom: 1px solid var(--esheet-muted);
}

.esheet-t-left {
  display: flex;
  align-items: center;
  gap: 8px;
  min-width: 0;
}

.esheet-t-center {
  display: flex;
  align-items: center;
  gap: 8px;
  justify-content: center;
  min-width: 0;
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
}

.esheet-t-right {
  display: flex;
  align-items: center;
  gap: 8px;
  justify-content: flex-end;
  min-width: 0;
}

.esheet-toolbar.settings {
  display: none;
  grid-template-columns: 1fr;
}

.esheet-toolbar.settings.open {
  display: grid;
}

/* Toolbar Buttons */
.esheet-toolbtn {
  appearance: none;
  border: 0;
  padding: 8px 12px;
  border-radius: 12px;
  font-weight: 700;
  color: var(--esheet-text);
  background: var(--esheet-muted);
  cursor: pointer;
  font-size: 14px;
  transition: all 0.15s ease;
}

.esheet-toolbtn[data-active="true"],
.esheet-toolbtn[data-selected="true"] {
  outline: 2px solid var(--esheet-accent);
  outline-offset: 0;
}

.esheet-toolbtn.close {
  background: #222;
  border: 1px solid #333;
}

.esheet-toolbtn.black {
  background: #111;
  border: 1px solid #333;
}

.esheet-toolbtn.erase {
  background: #1f242b;
  border: 1px solid #2c333d;
}

.esheet-toolbtn.red {
  background: #2b1416;
  border: 1px solid #5f1d1d;
}

.esheet-toolbtn.blue {
  background: #17233a;
  border: 1px solid #213457;
}

.esheet-toolbtn.settingsbtn {
  background: #1e2430;
  border: 1px solid #2b3442;
}

.esheet-toolbtn.danger {
  background: #3a1010;
  border: 1px solid #5f1d1d;
  color: #ffdede;
}

.esheet-toolbtn.overview {
  background: #16301f;
  border: 1px solid #2e6040;
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 8px 10px;
}

.esheet-overview-glyph {
  font-size: 16px;
  line-height: 1;
}

.esheet-overview-label {
  display: none;
  margin-left: 4px;
  font-weight: 700;
}

@media (min-width: 1024px) {
  .esheet-overview-label {
    display: inline;
  }
}

/* Icon Buttons */
.esheet-iconbtn {
  width: 38px;
  height: 38px;
  padding: 0;
  display: grid;
  place-items: center;
  border-radius: 10px;
  flex: 0 0 auto;
  min-width: 38px;
}

.esheet-iconbtn svg {
  width: 18px;
  height: 18px;
  stroke: currentColor;
  fill: none;
  stroke-width: 2;
}

/* Run Buttons */
.esheet-runbtn {
  width: 38px;
  height: 38px;
  display: grid;
  place-items: center;
  font-weight: 800;
  font-size: 14px;
  border-radius: 10px;
  flex: 0 0 auto;
  min-width: 38px;
}

.esheet-runbtn span {
  line-height: 1;
}

/* Input Fields */
.esheet-field {
  display: flex;
  align-items: center;
  gap: 6px;
  background: #1b2028;
  border: 1px solid #2d333d;
  border-radius: 10px;
  padding: 6px 8px;
  flex: 0 0 auto;
}

.esheet-field label {
  font-size: 11px;
  color: #b9c3d1;
  white-space: nowrap;
}

.esheet-field input {
  background: transparent;
  border: 0;
  color: #fff;
  outline: none;
  font-size: 13px;
  min-width: 0;
}

.esheet-field input[type="number"] {
  width: 72px;
}

.esheet-field input[type="text"] {
  width: 160px;
}

.esheet-field.active {
  outline: 2px solid var(--esheet-accent);
  outline-offset: 0;
}

/* Colored borders for score fields matching run buttons */
.esheet-field.run1 {
  border: 2px solid #5f1d1d;
  background: #2b1416;
}

.esheet-field.run1.active {
  border: 2px solid #ef4444;
  background: #3a1a1d;
  outline: 2px solid #ef4444;
  outline-offset: 0;
}

.esheet-field.run2 {
  border: 2px solid #213457;
  background: #17233a;
}

.esheet-field.run2.active {
  border: 2px solid #3b82f6;
  background: #1e2f4a;
  outline: 2px solid #3b82f6;
  outline-offset: 0;
}

/* Canvas Wrapper */
.esheet-canvas-wrap {
  position: relative;
  background: #fff;
  min-height: 0;
  overflow: hidden;
}

#esheetCanvas,
.esheet-canvas {
  touch-action: none;
  display: block;
  width: 100%;
  height: 100%;
  background: #fff;
  cursor: crosshair;
}

/* Overview Mode */
.esheet-overview {
  display: none;
  padding: 12px;
  overflow: auto;
  box-sizing: border-box;
  min-height: 0;
  height: 100%;
  background: var(--esheet-bg);
}

.esheet-grid {
  display: grid;
  grid-template-columns: repeat(3, minmax(220px, 1fr));
  gap: 12px;
  align-content: start;
  min-height: 0;
  max-width: 1600px;
  margin: 0 auto;
}

@media (max-width: 768px) {
  .esheet-grid {
    grid-template-columns: repeat(2, minmax(150px, 1fr));
  }
}

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

/* Cards in Overview */
.esheet-card {
  position: relative;
  padding: 0;
  border: none;
  background: transparent;
  cursor: pointer;
  display: block;
}

.esheet-thumb {
  position: relative;
  display: block;
  width: 100%;
  aspect-ratio: var(--esheet-overview-ar);
  background: #fff;
  border: 1px solid #1e2836;
  border-radius: 12px;
  overflow: hidden;
}

.esheet-thumb img {
  width: 100%;
  height: 100%;
  object-fit: contain;
  display: block;
}

/* Chips/Badges */
.esheet-chip {
  position: absolute;
  z-index: 2;
  background: rgba(17, 24, 39, 0.75);
  color: #e5eefc;
  border: 1px solid rgba(148, 163, 184, 0.35);
  padding: 4px 8px;
  border-radius: 999px;
  font-size: 12px;
  font-weight: 700;
  pointer-events: none;
}

.esheet-chip.name {
  top: 8px;
  left: 8px;
}

.esheet-chip.r1 {
  bottom: 8px;
  left: 8px;
}

.esheet-chip.r2 {
  bottom: 8px;
  right: 8px;
}

/* Group Header in Overview */
.esheet-group-header {
  grid-column: 1 / -1;
  padding: 12px 8px 8px;
  color: var(--esheet-text);
  font-weight: 700;
  font-size: 14px;
  border-bottom: 1px solid var(--esheet-muted);
  margin-bottom: 4px;
}

/* Settings Row */
.esheet-setting-group {
  display: flex;
  gap: 10px;
  padding: 8px 10px;
  flex-wrap: wrap;
}

/* Loading Indicator */
.esheet-loading {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 100%;
  color: var(--esheet-text);
  font-size: 16px;
}

/* Group Selector */
.esheet-group-select {
  background: #1b2028;
  border: 1px solid #2d333d;
  border-radius: 10px;
  padding: 6px 12px;
  color: var(--esheet-text);
  font-size: 13px;
  outline: none;
  cursor: pointer;
  max-width: 200px;
}

.esheet-group-select:focus {
  outline: 2px solid var(--esheet-accent);
  outline-offset: 0;
}

/* Athlete Info Display */
.esheet-athlete-info {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  min-width: 120px;
}

.esheet-athlete-name {
  font-size: 14px;
  font-weight: 600;
  color: var(--esheet-text);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 180px;
}

.esheet-athlete-country {
  font-size: 11px;
  color: #8b95a5;
}

/* Sync Status */
.esheet-sync-status {
  font-size: 10px;
  padding: 2px 6px;
  border-radius: 4px;
  background: #1a3d1a;
  color: #50cd89;
  transition: all 0.2s ease;
}

.esheet-sync-status.saving {
  background: #3d3a1a;
  color: #f0c14b;
}

.esheet-sync-status.error {
  background: #3d1a1a;
  color: #ef4444;
}

.esheet-sync-status.offline {
  background: #1a2d3d;
  color: #60a5fa;
  animation: offline-pulse 2s ease-in-out infinite;
}

.esheet-sync-status.syncing {
  background: #2d1a3d;
  color: #a78bfa;
  animation: syncing-spin 1s linear infinite;
}

@keyframes offline-pulse {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.6; }
}

@keyframes syncing-spin {
  0% { opacity: 0.6; }
  50% { opacity: 1; }
  100% { opacity: 0.6; }
}

/* ============================================================================
   Responsive Layout - iPad Optimized
   ============================================================================ */

/* Default desktop: single row */
.esheet-toolbar {
  display: flex;
  flex-wrap: nowrap;
  align-items: center;
  gap: 12px;
  padding: 10px 16px;
}

.esheet-t-left {
  display: flex;
  align-items: center;
  gap: 10px;
  flex-shrink: 0;
}

.esheet-t-center {
  display: flex;
  align-items: center;
  gap: 10px;
  flex: 1;
  justify-content: center;
  min-width: 0;
}

.esheet-t-right {
  display: flex;
  align-items: center;
  gap: 10px;
  flex-shrink: 0;
}

/* iPad landscape and smaller */
@media (max-width: 1024px) {
  .esheet-toolbar {
    flex-wrap: wrap;
    gap: 10px;
    padding: 10px 12px;
  }

  .esheet-t-left {
    order: 1;
    flex: 0 0 auto;
  }

  .esheet-t-right {
    order: 2;
    flex: 1;
    justify-content: flex-end;
  }

  .esheet-t-center {
    order: 3;
    flex: 1 1 100%;
    justify-content: space-between;
    padding-top: 10px;
    border-top: 1px solid var(--esheet-muted);
    margin-top: 6px;
    gap: 8px;
  }

  .esheet-group-select {
    max-width: 200px;
    font-size: 13px;
  }

  .esheet-athlete-info {
    min-width: 140px;
    flex: 1;
  }

  .esheet-athlete-name {
    font-size: 14px;
    max-width: 200px;
  }
}

/* Larger touch targets for touch devices */
@media (pointer: coarse) {
  .esheet-toolbtn {
    min-height: 44px;
    min-width: 44px;
  }

  .esheet-iconbtn {
    width: 44px;
    height: 44px;
    min-width: 44px;
  }

  .esheet-runbtn {
    width: 44px;
    height: 44px;
    min-width: 44px;
  }

  .esheet-field {
    min-height: 44px;
  }

  .esheet-group-select {
    min-height: 44px;
  }
}

/* Small phones */
@media (max-width: 480px) {
  .esheet-t-center {
    gap: 6px;
  }

  .esheet-field input[type="number"] {
    width: 56px;
  }

  .esheet-athlete-name {
    max-width: 100px;
    font-size: 12px;
  }

  .esheet-overview-label {
    display: none !important;
  }
}

/* ============================================================================
   Publish Button
   ============================================================================ */
.esheet-toolbtn.publish {
  background: linear-gradient(135deg, #50cd89 0%, #3bb876 100%);
  color: white;
  font-weight: 700;
  border: 1px solid #2d9a5e;
}

.esheet-toolbtn.publish:hover {
  background: linear-gradient(135deg, #3bb876 0%, #2d9a5e 100%);
}

.esheet-toolbtn.publish.published {
  background: linear-gradient(135deg, #2d9a5e 0%, #1f7847 100%);
  border-color: #1f7847;
}

.esheet-toolbtn.publish:disabled {
  background: var(--esheet-muted);
  color: #666;
  cursor: not-allowed;
  border-color: #333;
}

/* Status Buttons Container */
.esheet-status-buttons {
  display: flex;
  gap: 6px;
  align-items: center;
}

/* FRS Button (Fall/Re-Start) */
.esheet-toolbtn.frs {
  background: linear-gradient(135deg, #ffc700 0%, #e5b300 100%);
  color: #1a1a1a;
  font-weight: 700;
  border: 1px solid #cca300;
  min-width: 38px;
  height: 38px;
  padding: 0 10px;
}

.esheet-toolbtn.frs:hover:not(:disabled) {
  background: linear-gradient(135deg, #e5b300 0%, #cca300 100%);
}

.esheet-toolbtn.frs:disabled {
  background: var(--esheet-muted);
  color: #666;
  cursor: not-allowed;
  border-color: #333;
}

.esheet-toolbtn.frs.active {
  background: linear-gradient(135deg, #cca300 0%, #997a00 100%);
}

/* DNS Button (Did Not Start) */
.esheet-toolbtn.dns {
  background: linear-gradient(135deg, #009ef7 0%, #0078c8 100%);
  color: white;
  font-weight: 700;
  border: 1px solid #0066aa;
  min-width: 38px;
  height: 38px;
  padding: 0 10px;
}

.esheet-toolbtn.dns:hover:not(:disabled) {
  background: linear-gradient(135deg, #0078c8 0%, #0066aa 100%);
}

.esheet-toolbtn.dns:disabled {
  background: var(--esheet-muted);
  color: #666;
  cursor: not-allowed;
  border-color: #333;
}

.esheet-toolbtn.dns.active {
  background: linear-gradient(135deg, #0066aa 0%, #004d80 100%);
}

/* ============================================================================
   Athlete Flag
   ============================================================================ */
.esheet-athlete-flag {
  height: 16px;
  width: auto;
  margin-left: 6px;
  border-radius: 2px;
  vertical-align: middle;
  box-shadow: 0 1px 2px rgba(0,0,0,0.3);
}

/* Flag container in athlete info */
.esheet-athlete-info {
  flex-direction: row;
  gap: 8px;
}

.esheet-athlete-country {
  display: flex;
  align-items: center;
}

/* ============================================================================
   Group Dropdown - Status Indicators
   ============================================================================ */
.esheet-group-select optgroup {
  font-weight: 700;
  font-size: 12px;
  color: #8b95a5;
  background: #1b2028;
}

.esheet-group-select option {
  padding: 8px 12px;
  font-size: 13px;
}

.esheet-group-select option.active-group {
  color: var(--esheet-accent);
  font-weight: 600;
}

.esheet-group-select option.completed-group {
  color: #50cd89;
}

.esheet-group-select option.completed-group::before {
  content: "✓ ";
}

/* ============================================================================
   Confirm Dialog
   ============================================================================ */
.esheet-confirm-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.6);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 10001;
  backdrop-filter: blur(2px);
}

.esheet-confirm-dialog {
  background: var(--esheet-bg);
  border: 2px solid rgba(80, 205, 137, 0.4);
  border-radius: 16px;
  padding: 24px 28px;
  max-width: 340px;
  width: 90%;
  text-align: center;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.5);
}

.esheet-confirm-dialog h4 {
  color: #50cd89;
  margin: 0 0 12px 0;
  font-size: 16px;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
}

.esheet-confirm-dialog p {
  color: var(--esheet-text);
  margin: 0 0 16px 0;
  font-size: 14px;
}

.esheet-confirm-actions {
  display: flex;
  gap: 12px;
  justify-content: center;
}

.esheet-confirm-btn {
  padding: 10px 20px;
  border-radius: 10px;
  font-weight: 600;
  font-size: 14px;
  border: none;
  cursor: pointer;
  transition: all 0.15s ease;
}

.esheet-confirm-btn:hover {
  transform: scale(1.02);
}

.esheet-confirm-btn.cancel {
  background: var(--esheet-muted);
  color: var(--esheet-text);
}

.esheet-confirm-btn.confirm {
  background: linear-gradient(135deg, #50cd89 0%, #3bb876 100%);
  color: white;
}
