"""QuSpin-branded dark stylesheet.

The palette and widget idioms follow the QTFM2 GUI (see ``STYLE REFERENCE``):
a #1E1E1E application background, #2D2D2D panels, #404040 inputs and borders,
QuSpin golden rod accents, and #BBBBBB body text.
"""

from __future__ import annotations

import sys
from pathlib import Path

# --- QTFM2 GUI palette -------------------------------------------------------
GOLDEN_ROD = "#FAA519"
GOLDEN_ROD_HOVER = "#FFB640"
GOLDEN_ROD_PRESSED = "#E09410"
SECONDARY_BLUE = "#65A8BE"

BACKGROUND_MAIN = "#1E1E1E"
BACKGROUND_SECONDARY = "#2D2D2D"
BACKGROUND_TERTIARY = "#404040"
BACKGROUND_ELEVATED = "#3A3A3A"
BORDER = "#404040"
BORDER_STRONG = "#555555"

TEXT_PRIMARY = "#BBBBBB"
TEXT_SECONDARY = "#888888"
WHITE = "#FFFFFF"

LED_CONNECTED = "#4CAF50"
LED_PENDING = GOLDEN_ROD
LED_DISCONNECTED = "#F44336"

# Kept because the sensor cards colour their status dot with it.
MUTED_TEXT = TEXT_SECONDARY

# --- Plot palette ------------------------------------------------------------
PLOT_BACKGROUND = BACKGROUND_MAIN
PLOT_AXIS = GOLDEN_ROD
PLOT_TEXT = TEXT_PRIMARY
PLOT_GRID_ALPHA = 0.22

# Trace colours belong to the plot slot, never to the sensor letter: every
# sensor A–H is drawn in QuSpin golden rod. Only the ASD tab, where all three
# traces share one plot, needs the second and third colours to tell them apart.
SENSOR_TRACE = GOLDEN_ROD
COMPARE_TRACE = SECONDARY_BLUE
DIFFERENCE_COLOR = "#8FD3A9"

def _assets_dir() -> Path:
    """Return the assets folder for both source and PyInstaller runs."""
    bundle = getattr(sys, "_MEIPASS", None)
    if bundle:
        return Path(bundle) / "assets"
    return Path(__file__).resolve().parent / "assets"


ASSETS_DIR = _assets_dir()
QUSPIN_LOGO_PATH = ASSETS_DIR / "quspin_logo.png"
APP_ICON_PATH = ASSETS_DIR / "qtfm.ico"


APP_STYLESHEET = f"""
QWidget {{
    color: {TEXT_PRIMARY};
    font-family: "Segoe UI", Arial, Helvetica, sans-serif;
    font-size: 10pt;
}}

QMainWindow, QWidget#mainBackground {{
    background-color: {BACKGROUND_MAIN};
}}

QLabel {{
    background: transparent;
}}

QToolTip {{
    color: {WHITE};
    background-color: {BACKGROUND_SECONDARY};
    border: 1px solid {GOLDEN_ROD};
    padding: 4px 6px;
}}

/* ---------------------------------------------------------------- panels */
QFrame#hero {{
    background-color: {BACKGROUND_SECONDARY};
    border: 2px solid {BORDER};
    border-radius: 8px;
}}

QLabel#appTitle {{
    color: {GOLDEN_ROD};
    font-size: 16pt;
    font-weight: 700;
}}

QLabel#appVersion {{
    color: {TEXT_SECONDARY};
    background-color: {BACKGROUND_TERTIARY};
    border: 1px solid {BORDER_STRONG};
    border-radius: 4px;
    padding: 2px 7px;
    font-size: 9pt;
    font-weight: 700;
}}

QLabel#appSubtitle {{
    color: {TEXT_PRIMARY};
    font-size: 10pt;
}}

QFrame#sensorCard, QFrame#plotCard {{
    background-color: {BACKGROUND_SECONDARY};
    border: 2px solid {BORDER};
    border-radius: 8px;
}}

QFrame#commandBar, QFrame#actionBar, QFrame#recordingHeader,
QFrame#timestampSyncBox, QFrame#syncWorkflowBox {{
    background-color: {BACKGROUND_MAIN};
    border: 1px solid {BORDER};
    border-radius: 5px;
}}

QDialog#sensorSetupWindow, QDialog#terminalWindow, QDialog#parseDialog {{
    background-color: {BACKGROUND_MAIN};
}}

QPlainTextEdit#terminalView {{
    background-color: {BACKGROUND_MAIN};
    color: {TEXT_PRIMARY};
    border: 1px solid {BORDER};
    border-radius: 5px;
    padding: 4px;
    font-family: Consolas, monospace;
    font-size: 9pt;
    selection-background-color: {GOLDEN_ROD};
    selection-color: {WHITE};
}}

QScrollArea#sensorScroll {{
    background-color: {BACKGROUND_MAIN};
    border: none;
}}

QScrollArea#sensorScroll > QWidget > QWidget {{
    background-color: {BACKGROUND_MAIN};
}}

/* ---------------------------------------------------------------- titles */
QLabel#sensorTitle {{
    color: {GOLDEN_ROD};
    font-size: 12pt;
    font-weight: 700;
}}

QLabel#plotTitle, QLabel#commandBarTitle, QLabel#syncSectionTitle {{
    color: {GOLDEN_ROD};
    font-size: 11pt;
    font-weight: 700;
}}

QLabel#commandBarTitle, QLabel#syncSectionTitle {{
    font-size: 10pt;
}}

QLabel#fieldLabel {{
    color: {TEXT_PRIMARY};
    font-weight: 700;
    font-size: 9pt;
}}

QLabel#stateLabel {{
    color: {TEXT_PRIMARY};
    font-size: 9.5pt;
    font-weight: 600;
}}

QLabel#detailLabel, QLabel#controlHint, QLabel#asdStatus {{
    color: {TEXT_SECONDARY};
    font-size: 9pt;
}}

QLabel#footer {{
    color: {TEXT_SECONDARY};
    font-size: 9pt;
}}

QLabel#serialNumber {{
    color: {BACKGROUND_MAIN};
    background-color: {TEXT_PRIMARY};
    border-radius: 4px;
    padding: 3px 8px;
    font-size: 9pt;
    font-weight: 700;
}}

/* ------------------------------------------------------------- live values */
QLabel#sensorValue, QLabel#sensorAValue, QLabel#sensorBValue,
QLabel#differenceValue {{
    color: {GOLDEN_ROD};
    font-family: Consolas, monospace;
    font-size: 9.5pt;
    font-weight: 700;
}}

QLabel#cardValue {{
    color: {GOLDEN_ROD};
    font-family: Consolas, monospace;
    font-size: 10pt;
    font-weight: 700;
}}

QLabel#sensorStatusBar {{
    color: {TEXT_PRIMARY};
    background-color: {BACKGROUND_MAIN};
    border: 1px solid {BORDER};
    border-radius: 4px;
    padding: 5px 9px;
    font-family: Consolas, monospace;
    font-size: 8.5pt;
}}

QLabel#cursorReadout {{
    color: {TEXT_PRIMARY};
    background-color: {BACKGROUND_MAIN};
    border: 1px solid {BORDER};
    border-left: 3px solid {GOLDEN_ROD};
    border-radius: 4px;
    padding: 5px 9px;
    font-family: Consolas, monospace;
    font-size: 9pt;
}}

/* -------------------------------------------------------- recording state */
QLabel#recordingLed {{
    background-color: {BACKGROUND_TERTIARY};
    border: 1px solid {BORDER_STRONG};
    border-radius: 6px;
}}

QLabel#recordingLed[active="true"] {{
    background-color: {LED_DISCONNECTED};
    border: 1px solid #FF8A86;
}}

QLabel#recordingTimer {{
    color: {TEXT_SECONDARY};
    font-family: Consolas, monospace;
    font-size: 9pt;
    font-weight: 700;
}}

QLabel#recordingTimer[active="true"] {{
    color: #FF8A86;
}}

/* ------------------------------------------------------------ sync status */
QLabel#timestampUnknown, QLabel#roleUnknown {{
    color: {TEXT_SECONDARY};
    font-size: 9pt;
}}

QLabel#timestampAvailable, QLabel#roleMaster, QLabel#roleSlave {{
    color: {TEXT_PRIMARY};
    font-size: 9pt;
    font-weight: 700;
}}

QLabel#timestampMissing {{
    color: {GOLDEN_ROD};
    font-size: 9pt;
    font-weight: 700;
}}

QLabel#timestampMetric {{
    color: {TEXT_PRIMARY};
    font-family: Consolas, monospace;
    font-size: 9pt;
    font-weight: 600;
}}

QLabel#timestampAlignmentWaiting {{
    color: {TEXT_SECONDARY};
    font-weight: 700;
    font-size: 9pt;
}}

QLabel#timestampAlignmentGood {{
    color: {LED_CONNECTED};
    font-weight: 700;
    font-size: 9pt;
}}

QLabel#timestampAlignmentBad {{
    color: {GOLDEN_ROD};
    font-weight: 700;
    font-size: 9pt;
}}

QLabel#syncStatus {{
    color: {TEXT_PRIMARY};
    font-size: 9pt;
}}

QLabel#syncStatus[complete="true"] {{
    color: {LED_CONNECTED};
    font-weight: 700;
}}

QLabel#wiringRule {{
    color: {GOLDEN_ROD};
    background-color: {BACKGROUND_SECONDARY};
    border: 1px solid {BORDER};
    border-radius: 4px;
    padding: 4px 8px;
    font-size: 8.5pt;
    font-weight: 700;
}}

QLabel#roleBadgeUnknown {{
    color: {TEXT_SECONDARY};
    background-color: {BACKGROUND_TERTIARY};
    border: 1px solid {BORDER_STRONG};
    border-radius: 4px;
    padding: 4px 9px;
    font-size: 8.5pt;
    font-weight: 700;
}}

QLabel#roleBadgeMaster, QLabel#roleBadgeSlave {{
    color: {BACKGROUND_MAIN};
    background-color: {GOLDEN_ROD};
    border: 1px solid {GOLDEN_ROD_HOVER};
    border-radius: 4px;
    padding: 4px 9px;
    font-size: 8.5pt;
    font-weight: 700;
}}

QLabel#roleBadgeSlave {{
    background-color: {SECONDARY_BLUE};
    border-color: #8CC2D3;
}}

/* ---------------------------------------------------------------- buttons */
QPushButton {{
    background-color: {GOLDEN_ROD};
    color: {WHITE};
    border: none;
    border-radius: 5px;
    padding: 7px 14px;
    font-weight: 700;
}}

QPushButton:hover {{
    background-color: {GOLDEN_ROD_HOVER};
}}

QPushButton:pressed {{
    background-color: {GOLDEN_ROD_PRESSED};
}}

QPushButton:disabled {{
    background-color: {BACKGROUND_SECONDARY};
    color: {TEXT_SECONDARY};
}}

QPushButton#connectButton {{
    min-width: 100px;
}}

QPushButton#connectButton[connected="true"] {{
    background-color: {BACKGROUND_TERTIARY};
    color: {TEXT_PRIMARY};
    border: 2px solid {GOLDEN_ROD};
}}

QPushButton#connectButton[connected="true"]:hover {{
    background-color: {BACKGROUND_ELEVATED};
    border-color: {GOLDEN_ROD_HOVER};
}}

QPushButton#syncButtonProminent {{
    background-color: {GOLDEN_ROD};
    color: {WHITE};
    border: 2px solid {GOLDEN_ROD_HOVER};
    border-radius: 5px;
    padding: 5px 18px;
    font-size: 10.5pt;
    font-weight: 700;
}}

QPushButton#syncButtonProminent:hover {{
    background-color: {GOLDEN_ROD_HOVER};
}}

QPushButton#syncButtonProminent:disabled {{
    background-color: {BACKGROUND_SECONDARY};
    color: {TEXT_SECONDARY};
    border-color: {BORDER};
}}

QPushButton#secondaryButton, QPushButton#deviceButton,
QPushButton#recordButton, QPushButton#helpButton,
QPushButton#roleButton, QPushButton#timestampButton {{
    background-color: {BACKGROUND_TERTIARY};
    color: {TEXT_PRIMARY};
    border: 1px solid {BORDER_STRONG};
    border-radius: 5px;
    padding: 5px 12px;
    font-weight: 700;
}}

QPushButton#roleButton, QPushButton#timestampButton {{
    font-size: 8.5pt;
    padding: 5px 10px;
}}

QPushButton#secondaryButton:hover, QPushButton#deviceButton:hover,
QPushButton#recordButton:hover, QPushButton#helpButton:hover,
QPushButton#roleButton:hover, QPushButton#timestampButton:hover {{
    background-color: {BACKGROUND_ELEVATED};
    border-color: {GOLDEN_ROD};
    color: {WHITE};
}}

QPushButton#secondaryButton:disabled, QPushButton#deviceButton:disabled,
QPushButton#recordButton:disabled, QPushButton#helpButton:disabled,
QPushButton#roleButton:disabled, QPushButton#timestampButton:disabled {{
    background-color: {BACKGROUND_SECONDARY};
    color: {TEXT_SECONDARY};
    border-color: {BORDER};
}}

QPushButton#roleButton[active="true"] {{
    background-color: {GOLDEN_ROD};
    color: {WHITE};
    border-color: {GOLDEN_ROD_HOVER};
}}

QPushButton#recordButton {{
    background-color: {GOLDEN_ROD};
    color: {WHITE};
    border: none;
}}

QPushButton#recordButton:hover {{
    background-color: {GOLDEN_ROD_HOVER};
    border: none;
}}

QPushButton#dangerButton {{
    background-color: {BACKGROUND_TERTIARY};
    color: #FF8A86;
    border: 1px solid #A95A53;
    border-radius: 5px;
    padding: 6px 12px;
    font-size: 8.5pt;
    font-weight: 700;
}}

QPushButton#dangerButton:hover {{
    background-color: #6D403D;
    color: {WHITE};
}}

QPushButton#dangerButton:disabled {{
    background-color: {BACKGROUND_SECONDARY};
    color: {TEXT_SECONDARY};
    border-color: {BORDER};
}}

/* ----------------------------------------------------------------- inputs */
QComboBox, QLineEdit, QSpinBox {{
    background-color: {BACKGROUND_TERTIARY};
    color: {TEXT_PRIMARY};
    border: 2px solid {GOLDEN_ROD};
    border-radius: 5px;
    padding: 4px 8px;
    min-height: 18px;
    selection-background-color: {GOLDEN_ROD};
    selection-color: {WHITE};
}}

QComboBox:hover, QLineEdit:hover, QSpinBox:hover,
QLineEdit:focus, QSpinBox:focus {{
    border-color: {GOLDEN_ROD_HOVER};
}}

QComboBox:disabled, QLineEdit:disabled, QSpinBox:disabled {{
    background-color: {BACKGROUND_SECONDARY};
    color: {TEXT_SECONDARY};
    border-color: {BORDER};
}}

/* ::drop-down / ::down-arrow are deliberately not styled. A stylesheet arrow
   needs an image resource, and styling the sub-control at all suppresses the
   arrow the native style would otherwise draw. */

QComboBox QAbstractItemView {{
    background-color: {BACKGROUND_SECONDARY};
    color: {TEXT_PRIMARY};
    selection-background-color: {GOLDEN_ROD};
    selection-color: {WHITE};
    border: 1px solid {BORDER};
    outline: none;
}}

QCheckBox {{
    color: {TEXT_PRIMARY};
    font-weight: 700;
    font-size: 9pt;
    spacing: 6px;
}}

QCheckBox::indicator {{
    width: 14px;
    height: 14px;
    border: 2px solid {GOLDEN_ROD};
    border-radius: 3px;
    background-color: {BACKGROUND_TERTIARY};
}}

QCheckBox::indicator:checked {{
    background-color: {GOLDEN_ROD};
}}

/* ------------------------------------------------------------------- tabs */
QTabWidget#plotTabs::pane {{
    border: none;
    background-color: transparent;
}}

QTabBar::tab {{
    background-color: {BACKGROUND_SECONDARY};
    color: {TEXT_SECONDARY};
    border: 1px solid {BORDER};
    border-bottom: none;
    border-top-left-radius: 5px;
    border-top-right-radius: 5px;
    min-width: 130px;
    padding: 7px 16px;
    margin-right: 2px;
    font-weight: 700;
}}

QTabBar::tab:selected {{
    background-color: {BACKGROUND_TERTIARY};
    color: {GOLDEN_ROD};
    border-color: {GOLDEN_ROD};
}}

QTabBar::tab:hover {{
    color: {TEXT_PRIMARY};
}}

/* --------------------------------------------------------------- scrolling */
QScrollBar:vertical {{
    background: {BACKGROUND_MAIN};
    width: 11px;
    margin: 0px;
}}

QScrollBar::handle:vertical {{
    background: {BACKGROUND_TERTIARY};
    border-radius: 5px;
    min-height: 28px;
}}

QScrollBar::handle:vertical:hover {{
    background: {GOLDEN_ROD};
}}

QScrollBar:horizontal {{
    background: {BACKGROUND_MAIN};
    height: 11px;
    margin: 0px;
}}

QScrollBar::handle:horizontal {{
    background: {BACKGROUND_TERTIARY};
    border-radius: 5px;
    min-width: 28px;
}}

QScrollBar::handle:horizontal:hover {{
    background: {GOLDEN_ROD};
}}

QScrollBar::add-line, QScrollBar::sub-line {{
    height: 0px;
    width: 0px;
}}

QScrollBar::add-page, QScrollBar::sub-page {{
    background: transparent;
}}
"""
